├── .gitignore ├── README.md ├── demo.gif ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── App.test.js ├── app └── store.js ├── features ├── Comments │ ├── Comments.js │ └── commentsSlice.js ├── QuestionDetail │ └── QuestionDetail.js └── Questions │ ├── Questions.js │ └── questionSlice.js ├── index.css ├── index.js ├── logo.svg ├── plus_icon.png ├── route.js ├── serviceWorker.js └── setupTests.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Modern React Redux Tutorial with Redux toolkit 2 | 3 | This Repo demostrates the Modern React Redux application development with Redux toolkit. 4 | 5 | ![](./demo.gif) 6 | 7 | ##### Using Create React App 8 | 9 | ``` 10 | npx create-react-app my-app --template redux 11 | ``` 12 | 13 | ##### An Existing App 14 | 15 | ``` 16 | # NPM 17 | npm install @reduxjs/toolkit 18 | 19 | # Yarn 20 | yarn add @reduxjs/toolkit 21 | ``` 22 | 23 | ##### To Run this App 24 | 25 | ``` 26 | npm install 27 | 28 | npm run start 29 | ``` -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganeshmani/modern-react-redux-tutorial/3adbc3e798128a20e7bb75fd7a0f3519b9251c16/demo.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-redux-toolkit", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@reduxjs/toolkit": "^1.3.4", 7 | "@testing-library/jest-dom": "^4.2.4", 8 | "@testing-library/react": "^9.5.0", 9 | "@testing-library/user-event": "^7.2.1", 10 | "react": "^16.13.1", 11 | "react-dom": "^16.13.1", 12 | "react-redux": "^7.2.0", 13 | "react-router-dom": "^5.1.2", 14 | "react-scripts": "3.4.1", 15 | "react-transition-group": "^4.3.0", 16 | "styled-components": "^5.0.1" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganeshmani/modern-react-redux-tutorial/3adbc3e798128a20e7bb75fd7a0f3519b9251c16/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React Redux App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganeshmani/modern-react-redux-tutorial/3adbc3e798128a20e7bb75fd7a0f3519b9251c16/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganeshmani/modern-react-redux-tutorial/3adbc3e798128a20e7bb75fd7a0f3519b9251c16/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-float infinite 3s ease-in-out; 13 | } 14 | } 15 | 16 | .App-header { 17 | min-height: 100vh; 18 | display: flex; 19 | flex-direction: column; 20 | align-items: center; 21 | justify-content: center; 22 | font-size: calc(10px + 2vmin); 23 | } 24 | 25 | .App-link { 26 | color: rgb(112, 76, 182); 27 | } 28 | 29 | @keyframes App-logo-float { 30 | 0% { 31 | transform: translateY(0); 32 | } 33 | 50% { 34 | transform: translateY(10px) 35 | } 36 | 100% { 37 | transform: translateY(0px) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import logo from "./logo.svg"; 3 | import "./App.css"; 4 | import Route from "./route"; 5 | 6 | function App() { 7 | return ( 8 |
9 | 10 |
11 | ); 12 | } 13 | 14 | export default App; 15 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import { Provider } from 'react-redux'; 4 | import store from './app/store'; 5 | import App from './App'; 6 | 7 | test('renders learn react link', () => { 8 | const { getByText } = render( 9 | 10 | 11 | 12 | ); 13 | 14 | expect(getByText(/learn/i)).toBeInTheDocument(); 15 | }); 16 | -------------------------------------------------------------------------------- /src/app/store.js: -------------------------------------------------------------------------------- 1 | import { configureStore } from "@reduxjs/toolkit"; 2 | import questionReducer from "../features/Questions/questionSlice"; 3 | import commentReducer from "../features/Comments/commentsSlice"; 4 | export default configureStore({ 5 | reducer: { 6 | questions: questionReducer, 7 | comments: commentReducer, 8 | }, 9 | }); 10 | -------------------------------------------------------------------------------- /src/features/Comments/Comments.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import styled from "styled-components"; 3 | import { useSelector, useDispatch } from "react-redux"; 4 | 5 | import { addComment } from "./commentsSlice"; 6 | 7 | const CommentsContainer = styled.div``; 8 | 9 | const CommentHeading = styled.h4``; 10 | 11 | const CommentLists = styled.ul` 12 | text-decoration: none; 13 | list-style: none; 14 | display: flex; 15 | flex-flow: column; 16 | padding: 1.75rem; 17 | max-height: 200px; 18 | overflow: auto; 19 | `; 20 | 21 | const AddCommentsInput = styled.input` 22 | width: 10%; 23 | height: 32px; 24 | border-radius: 8px; 25 | `; 26 | 27 | const CommentListItem = styled.div` 28 | padding: 10px; 29 | `; 30 | 31 | const CommentTitle = styled.div``; 32 | 33 | const Comments = ({ id }) => { 34 | const [comment, setComment] = useState(""); 35 | 36 | const comments = useSelector((state) => { 37 | let comments = state.comments.filter((comment) => comment.questionId == id); 38 | 39 | return comments; 40 | }); 41 | const dispatch = useDispatch(); 42 | 43 | const onAddComment = (e) => { 44 | if (e.key !== "Enter") { 45 | return; 46 | } 47 | 48 | if (e.key === "Enter") { 49 | let data = { 50 | id: comments && comments.length > 0 ? comments.length + 1 : 1, 51 | comment: comment, 52 | questionId: id, 53 | }; 54 | 55 | dispatch(addComment(data)); 56 | setComment(""); 57 | } 58 | }; 59 | return ( 60 |
61 | 62 | Comments 63 | 64 | {comments && 65 | comments.map((comment) => ( 66 | 67 | {comment.comment} 68 | 69 | ))} 70 | 71 | setComment(e.target.value)} 75 | onKeyPress={onAddComment} 76 | /> 77 | 78 |
79 | ); 80 | }; 81 | 82 | export default Comments; 83 | -------------------------------------------------------------------------------- /src/features/Comments/commentsSlice.js: -------------------------------------------------------------------------------- 1 | import { createSlice } from "@reduxjs/toolkit"; 2 | 3 | export const commentSlice = createSlice({ 4 | name: "comments", 5 | initialState: [], 6 | reducers: { 7 | addComment: (state, action) => { 8 | console.log("Action", action); 9 | state = state.push({ 10 | id: action.payload.id, 11 | comment: action.payload.comment, 12 | questionId: action.payload.questionId, 13 | }); 14 | }, 15 | editComment: (state, action) => { 16 | state = state.map((comment) => { 17 | if (comment.id == action.payload.id) { 18 | return { 19 | id: action.payload.id, 20 | comment: action.payload.comment, 21 | questionId: action.payload.questionId, 22 | }; 23 | } else { 24 | return { 25 | ...comment, 26 | }; 27 | } 28 | }); 29 | return state; 30 | }, 31 | removeComment: (state, action) => { 32 | state = state.filter((comment) => comment.id !== action.payload.id); 33 | 34 | return state; 35 | }, 36 | }, 37 | }); 38 | 39 | export const { addComment, editComment, removeComment } = commentSlice.actions; 40 | 41 | export const comments = (state) => state.comments; 42 | 43 | export default commentSlice.reducer; 44 | -------------------------------------------------------------------------------- /src/features/QuestionDetail/QuestionDetail.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import styled from "styled-components"; 3 | import { useSelector } from "react-redux"; 4 | import { useParams, useHistory } from "react-router-dom"; 5 | 6 | import Comments from "../Comments/Comments"; 7 | 8 | const Container = styled.div` 9 | width: 100vw; 10 | height: 100vh; 11 | background-color: #efecec; 12 | `; 13 | 14 | const QuestionsContainer = styled.div` 15 | display: flex; 16 | flex-flow: column; 17 | padding: 3.75rem 5rem; 18 | width: 20%; 19 | box-shadow: 0 0.125rem 0.375rem rgba(0, 0, 0, 0.2); 20 | border-radius: 0.3125rem; 21 | background: #fff; 22 | margin: auto; 23 | `; 24 | 25 | const Heading = styled.h2``; 26 | 27 | const QuestionLabel = styled.h4` 28 | font-weight: 300; 29 | `; 30 | 31 | const QuestionDetail = (props) => { 32 | const { id } = useParams(); 33 | if (!id) { 34 | } 35 | const questionDetail = useSelector((state) => { 36 | let question = state.questions.find((question) => question.id == id); 37 | 38 | return question; 39 | }); 40 | 41 | return ( 42 | 43 | 44 | Title: 45 | {questionDetail && questionDetail.title} 46 | Body: 47 | {questionDetail && questionDetail.body} 48 | 49 | {questionDetail ? : null} 50 | 51 | ); 52 | }; 53 | 54 | export default QuestionDetail; 55 | -------------------------------------------------------------------------------- /src/features/Questions/Questions.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { useDispatch, useSelector } from "react-redux"; 3 | import { CSSTransition } from "react-transition-group"; 4 | import { Link } from "react-router-dom"; 5 | import plus_icon from "../../plus_icon.png"; 6 | import styled from "styled-components"; 7 | 8 | import { 9 | selectQuestions, 10 | addQuestion, 11 | removeQuestion, 12 | editQuestion, 13 | } from "./questionSlice"; 14 | 15 | const Container = styled.div` 16 | width: 100vw; 17 | height: 100vh; 18 | background-color: #efecec; 19 | `; 20 | 21 | const QuestionsContainer = styled.div` 22 | display: flex; 23 | flex-flow: column; 24 | padding: 3.75rem 5rem; 25 | `; 26 | 27 | const QuestionListItem = styled.div` 28 | padding: 10px; 29 | `; 30 | 31 | const QuestionTitle = styled.div``; 32 | 33 | const AddQuestionButtonContainer = styled.div` 34 | cursor: pointer; 35 | width: 10%; 36 | margin: auto; 37 | display: flex; 38 | padding: 10px; 39 | justify-content: center; 40 | background-color: #edf2f7; 41 | border-radius: 8px; 42 | font-weight: 600; 43 | 44 | &:hover { 45 | background-color: #e2e8f0; 46 | } 47 | `; 48 | 49 | const AddQuestionIcon = styled.img` 50 | width: 14px; 51 | height: 14px; 52 | `; 53 | 54 | const AddQuestionName = styled.div``; 55 | 56 | const FormContainer = styled.div` 57 | padding: 0.625rem 1rem; 58 | /* margin: 1.25rem -1.25rem; */ 59 | box-shadow: 0 0.125rem 0.375rem rgba(0, 0, 0, 0.2); 60 | border-radius: 0.3125rem; 61 | background: #fff; 62 | width: 50%; 63 | margin: 10px auto; 64 | `; 65 | 66 | const FormContainerDiv = styled.div` 67 | display: flex; 68 | align-items: center; 69 | flex-direction: column; 70 | `; 71 | 72 | const FormLabel = styled.label``; 73 | 74 | const FormContainerTitleInput = styled.input` 75 | flex: 1; 76 | font-size: 0.9375rem; 77 | width: 50%; 78 | margin: auto; 79 | height: 100px; 80 | `; 81 | 82 | const FormContainerBodyInput = styled.input` 83 | flex: 1; 84 | font-size: 0.9375rem; 85 | padding: 0.75rem 0.5rem; 86 | width: 50%; 87 | margin: auto; 88 | height: 100px; 89 | `; 90 | 91 | const AddQuestionButton = styled.button``; 92 | 93 | const Questions = () => { 94 | const [showAddQuestion, setShowAddQuestion] = useState(false); 95 | 96 | const [title, setTitle] = useState(""); 97 | const [body, setBody] = useState(""); 98 | const questions = useSelector(selectQuestions); 99 | const dispatch = useDispatch(); 100 | 101 | const onSubmit = () => { 102 | let data = { 103 | id: questions.length + 1, 104 | title: title, 105 | body, 106 | }; 107 | 108 | dispatch(addQuestion(data)); 109 | setShowAddQuestion(false); 110 | setTitle(""); 111 | setBody(""); 112 | }; 113 | 114 | return ( 115 | 116 | 117 | {questions && questions.length > 0 ? ( 118 | questions.map((question, index) => { 119 | return ( 120 | 121 | 122 | {question.title} 123 | 124 | 125 | ); 126 | }) 127 | ) : ( 128 |
No Data Found
129 | )} 130 |
131 | setShowAddQuestion(true)}> 132 | 133 | Add Question 134 | 135 | 136 | {showAddQuestion ? ( 137 | 138 | 139 | Title 140 | setTitle(e.target.value)} 144 | /> 145 | 146 | 147 | 148 | Body 149 | setBody(e.target.value)} 153 | /> 154 | 155 | Submit 156 | 157 | ) : ( 158 | "" 159 | )} 160 |
161 | ); 162 | }; 163 | 164 | export default Questions; 165 | -------------------------------------------------------------------------------- /src/features/Questions/questionSlice.js: -------------------------------------------------------------------------------- 1 | import { createSlice } from "@reduxjs/toolkit"; 2 | 3 | export const questionSlice = createSlice({ 4 | name: "questions", 5 | initialState: [], 6 | reducers: { 7 | addQuestion: (state, action) => { 8 | state = state.push({ 9 | id: action.payload.id, 10 | title: action.payload.title, 11 | body: action.payload.body, 12 | }); 13 | // return state; 14 | }, 15 | editQuestion: (state, action) => { 16 | state = state.map((question) => { 17 | if (question.id === action.payload.id) { 18 | return { 19 | id: action.payload.id, 20 | title: action.payload.title, 21 | body: action.payload.body, 22 | }; 23 | } else { 24 | return { 25 | ...question, 26 | }; 27 | } 28 | }); 29 | 30 | return state; 31 | }, 32 | removeQuestion: (state, action) => { 33 | state = state.filter((question) => question.id !== action.payload.id); 34 | return state; 35 | }, 36 | }, 37 | }); 38 | 39 | export const { 40 | addQuestion, 41 | editQuestion, 42 | removeQuestion, 43 | } = questionSlice.actions; 44 | 45 | export const selectQuestions = (state) => state.questions; 46 | 47 | export default questionSlice.reducer; 48 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import store from './app/store'; 6 | import { Provider } from 'react-redux'; 7 | import * as serviceWorker from './serviceWorker'; 8 | 9 | ReactDOM.render( 10 | 11 | 12 | 13 | 14 | , 15 | document.getElementById('root') 16 | ); 17 | 18 | // If you want your app to work offline and load faster, you can change 19 | // unregister() to register() below. Note this comes with some pitfalls. 20 | // Learn more about service workers: https://bit.ly/CRA-PWA 21 | serviceWorker.unregister(); 22 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/plus_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ganeshmani/modern-react-redux-tutorial/3adbc3e798128a20e7bb75fd7a0f3519b9251c16/src/plus_icon.png -------------------------------------------------------------------------------- /src/route.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; 3 | import Questions from "./features/Questions/Questions"; 4 | import QuestionDetail from "./features/QuestionDetail/QuestionDetail"; 5 | const Routes = () => { 6 | return ( 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | ); 18 | }; 19 | 20 | export default Routes; 21 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready.then(registration => { 134 | registration.unregister(); 135 | }); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | --------------------------------------------------------------------------------