├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── components ├── component_1.js ├── component_2.js └── wrapComponents │ ├── component_1_w.js │ └── component_2_w.js ├── index.js ├── serviceWorker.js └── store ├── actionCreators ├── action_1.js └── action_2.js ├── actions ├── action_1.js └── action_2.js ├── initialState.js ├── mapDispatchToProps.js ├── mapStateToProps.js ├── reducers ├── reducer_1.js ├── reducer_2.js └── reducers.js └── store.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = tab 7 | indent_size = 4 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | end_of_line = lf 12 | max_line_length = off 13 | 14 | [package.json] 15 | indent_size = 2 -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ruslan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

Basic react redux app

4 |

A Predictable State Container for JS Apps

5 |

A simple React Redux application for example.

6 |

Why you should use redux?

7 | 13 |
14 | 15 |

see my article on Redux and React-redux on habr

16 | 17 | 18 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 19 | 20 | ## Available Scripts 21 | 22 | In the project directory, you can run: 23 | 24 | ### `npm start` 25 | 26 | Runs the app in the development mode.
27 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 28 | 29 | The page will reload if you make edits.
30 | You will also see any lint errors in the console. 31 | 32 | ### `npm test` 33 | 34 | Launches the test runner in the interactive watch mode.
35 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 36 | 37 | ### `npm run build` 38 | 39 | Builds the app for production to the `build` folder.
40 | It correctly bundles React in production mode and optimizes the build for the best performance. 41 | 42 | The build is minified and the filenames include the hashes.
43 | Your app is ready to be deployed! 44 | 45 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 46 | 47 | ### `npm run eject` 48 | 49 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 50 | 51 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 52 | 53 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 54 | 55 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 56 | 57 | ## Learn More 58 | 59 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 60 | 61 | To learn React, check out the [React documentation](https://reactjs.org/). 62 | 63 | ### Code Splitting 64 | 65 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 66 | 67 | ### Analyzing the Bundle Size 68 | 69 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 70 | 71 | ### Making a Progressive Web App 72 | 73 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 74 | 75 | ### Advanced Configuration 76 | 77 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 78 | 79 | ### Deployment 80 | 81 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 82 | 83 | ### `npm run build` fails to minify 84 | 85 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 86 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "basical-react-redux-app", 3 | "version": "1.0.0", 4 | "private": true, 5 | "author": "Ruslan Valimuhametov", 6 | "license": "MIT", 7 | "description": "Example React-redux application", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/Ruslan101/Basical-react-redux-app" 11 | }, 12 | "keywords": [ 13 | "react", 14 | "node", 15 | "redux", 16 | "js" 17 | ], 18 | "bugs": { 19 | "url": "https://github.com/Ruslan101/Basical-react-redux-app/issues" 20 | }, 21 | "dependencies": { 22 | "@testing-library/jest-dom": "^4.2.4", 23 | "@testing-library/react": "^9.5.0", 24 | "@testing-library/user-event": "^7.2.1", 25 | "react": "^16.13.1", 26 | "react-dom": "^16.13.1", 27 | "react-scripts": "3.4.1" 28 | }, 29 | "scripts": { 30 | "start": "react-scripts start", 31 | "build": "react-scripts build", 32 | "test": "react-scripts test", 33 | "eject": "react-scripts eject" 34 | }, 35 | "eslintConfig": { 36 | "extends": "react-app" 37 | }, 38 | "browserslist": { 39 | "production": [ 40 | ">0.2%", 41 | "not dead", 42 | "not op_mini all" 43 | ], 44 | "development": [ 45 | "last 1 chrome version", 46 | "last 1 firefox version", 47 | "last 1 safari version" 48 | ] 49 | }, 50 | "devDependencies": { 51 | "react-redux": "^7.2.0", 52 | "redux": "^4.0.5" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ruslan101/Basical-react-redux-app/89339149afc5269f0b8619f0b6668993fcd260f5/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |

32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ruslan101/Basical-react-redux-app/89339149afc5269f0b8619f0b6668993fcd260f5/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ruslan101/Basical-react-redux-app/89339149afc5269f0b8619f0b6668993fcd260f5/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 | Disallow: 4 | -------------------------------------------------------------------------------- /src/components/component_1.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class Component_1 extends React.Component { 4 | handleAction = () => { 5 | this.props.change_value_1(!this.props.value_1); 6 | } 7 | render() { 8 | return ( 9 | <> 10 |

{this.props.value_1 ? "Off" : "On"}

11 | 12 | 13 | ); 14 | } 15 | } 16 | 17 | export default Component_1; -------------------------------------------------------------------------------- /src/components/component_2.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | class Component_1 extends React.Component { 4 | handleAction = () => { 5 | this.props.change_value_2(!this.props.value_2); 6 | } 7 | render() { 8 | return ( 9 | <> 10 |

{this.props.value_2 ? "Off" : "On"}

11 | 12 | 13 | ); 14 | } 15 | } 16 | 17 | export default Component_1; -------------------------------------------------------------------------------- /src/components/wrapComponents/component_1_w.js: -------------------------------------------------------------------------------- 1 | import { connect } from 'react-redux'; 2 | import Component_1 from '../component_1'; 3 | import mapStateToProps from '../../store/mapStateToProps'; 4 | import mapDispatchToProps from '../../store/mapDispatchToProps'; 5 | 6 | const COMPONENT_1_W = connect(mapStateToProps("Component_1"), mapDispatchToProps("Component_1"))(Component_1); 7 | 8 | export default COMPONENT_1_W; -------------------------------------------------------------------------------- /src/components/wrapComponents/component_2_w.js: -------------------------------------------------------------------------------- 1 | import { connect } from 'react-redux'; 2 | import Component_2 from '../component_2'; 3 | import mapStateToProps from '../../store/mapStateToProps'; 4 | import mapDispatchToProps from '../../store/mapDispatchToProps'; 5 | 6 | const COMPONENT_2_W = connect(mapStateToProps("Component_2"), mapDispatchToProps("Component_2"))(Component_2); 7 | 8 | export default COMPONENT_2_W; -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import { Provider } from 'react-redux'; 4 | import * as serviceWorker from './serviceWorker'; 5 | import store from './store/store'; 6 | import COMPONENT_1_W from './components/wrapComponents/component_1_w'; 7 | import COMPONENT_2_W from './components/wrapComponents/component_2_w'; 8 | 9 | ReactDOM.render( 10 | 11 | 12 | 13 | , 14 | document.getElementById('root')); 15 | 16 | // If you want your app to work offline and load faster, you can change 17 | // unregister() to register() below. Note this comes with some pitfalls. 18 | // Learn more about service workers: https://bit.ly/CRA-PWA 19 | serviceWorker.unregister(); -------------------------------------------------------------------------------- /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 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/store/actionCreators/action_1.js: -------------------------------------------------------------------------------- 1 | import ACTION_1 from '../actions/action_1'; 2 | 3 | function action_1(value) { 4 | return { 5 | type: ACTION_1, 6 | value_1: value 7 | }; 8 | } 9 | 10 | export default action_1; -------------------------------------------------------------------------------- /src/store/actionCreators/action_2.js: -------------------------------------------------------------------------------- 1 | import ACTION_2 from '../actions/action_2'; 2 | 3 | function action_2(value) { 4 | return { 5 | type: ACTION_2, 6 | value_2: value 7 | }; 8 | } 9 | 10 | export default action_2; -------------------------------------------------------------------------------- /src/store/actions/action_1.js: -------------------------------------------------------------------------------- 1 | const ACTION_1 = "ACTION_1"; 2 | 3 | export default ACTION_1; -------------------------------------------------------------------------------- /src/store/actions/action_2.js: -------------------------------------------------------------------------------- 1 | const ACTION_2 = "ACTION_2"; 2 | 3 | export default ACTION_2; -------------------------------------------------------------------------------- /src/store/initialState.js: -------------------------------------------------------------------------------- 1 | const initialState = { 2 | value_1: false, 3 | value_2: false 4 | }; 5 | 6 | export default initialState; -------------------------------------------------------------------------------- /src/store/mapDispatchToProps.js: -------------------------------------------------------------------------------- 1 | import { bindActionCreators } from 'redux'; 2 | import action_1 from './actionCreators/action_1'; 3 | import action_2 from './actionCreators/action_2'; 4 | 5 | function mapDispatchToProps(component) { 6 | switch (component) { 7 | case "Component_1": return function (dispatch) { 8 | return { 9 | change_value_1: bindActionCreators(action_1, dispatch) 10 | }; 11 | }; 12 | case "Component_2": return function (dispatch) { 13 | return { 14 | change_value_2: bindActionCreators(action_2, dispatch) 15 | }; 16 | }; 17 | default: return undefined; 18 | } 19 | } 20 | 21 | export default mapDispatchToProps; -------------------------------------------------------------------------------- /src/store/mapStateToProps.js: -------------------------------------------------------------------------------- 1 | function mapStateToProps(component) { 2 | switch (component) { 3 | case "Component_1": { 4 | return function (state) { 5 | return { 6 | value_1: state.value_1 7 | }; 8 | } 9 | } 10 | case "Component_2": { 11 | return function (state) { 12 | return { 13 | value_2: state.value_2 14 | }; 15 | } 16 | } 17 | default: return undefined; 18 | } 19 | } 20 | 21 | export default mapStateToProps; -------------------------------------------------------------------------------- /src/store/reducers/reducer_1.js: -------------------------------------------------------------------------------- 1 | import ACTION_1 from '../actions/action_1'; 2 | import initialState from '../initialState'; 3 | 4 | export default function value_1(state = initialState.value_1, action) { 5 | switch(action.type) { 6 | case ACTION_1: return action.value_1; 7 | 8 | default: return state; 9 | } 10 | } -------------------------------------------------------------------------------- /src/store/reducers/reducer_2.js: -------------------------------------------------------------------------------- 1 | import ACTION_2 from '../actions/action_2'; 2 | import initialState from '../initialState'; 3 | 4 | export default function value_2(state = initialState.value_2, action) { 5 | switch(action.type) { 6 | case ACTION_2: return action.value_2; 7 | 8 | default: return state; 9 | } 10 | } -------------------------------------------------------------------------------- /src/store/reducers/reducers.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | import value_1 from './reducer_1'; 3 | import value_2 from './reducer_2'; 4 | 5 | const reducers = combineReducers({ 6 | value_1, 7 | value_2 8 | }); 9 | 10 | export default reducers; -------------------------------------------------------------------------------- /src/store/store.js: -------------------------------------------------------------------------------- 1 | import { createStore } from 'redux'; 2 | import reducers from './reducers/reducers'; 3 | 4 | const store = createStore(reducers); 5 | 6 | export default store; 7 | 8 | //store.subscribe(() => console.info(store.getState())) // Для отладки :D --------------------------------------------------------------------------------