├── .gitignore
├── LICENSE
├── README-more-stats.md
├── README.md
├── reactn
├── .gitignore
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ └── manifest.json
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ └── serviceWorker.js
└── yarn.lock
└── redux
├── .gitignore
├── README.md
├── package.json
├── public
├── favicon.ico
├── index.html
└── manifest.json
├── src
├── App.css
├── App.js
├── App.test.js
├── index.css
├── index.js
├── logo.svg
└── serviceWorker.js
└── yarn.lock
/.gitignore:
--------------------------------------------------------------------------------
1 | /create-react-app
2 | /reactn/build
3 | /reactn/node_modules
4 | /redux/build
5 | /redux/node_modules
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Charles Stover
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.
22 |
--------------------------------------------------------------------------------
/README-more-stats.md:
--------------------------------------------------------------------------------
1 | The following statistics were deemed unimportant for the main README and are listed here for archival.
2 |
3 | ## Foundation Testing
4 |
5 | ### Development Size in Bytes
6 |
7 | The development size in bytes measures how much disk space is required to implement one of these global state management packages.
8 | This size was determined prior to the adjustments listed under the Methodology section above and therefore only takes into account _dependencies_.
9 | This removes white-space bias, however negligible, and focuses solely on the size of ReactN and Redux.
10 |
11 | The baseline, `create-react-app`, is 116,852,648 bytes. The following statistics _include_ `create-react-app`'s files.
12 |
13 | | Application | Size | Size Diff. | CRA Diff. |
14 | | ----------- | ----------- | ---------------- | ---------------- |
15 | | reactn | 117,111,833 | -272,831 (-0.2%) | +259,185 (+0.2%) |
16 | | redux | 117,384,664 | +272,831 (+0.2%) | +532,016 (+0.5%) |
17 |
18 | Comparisons were determined using `ls -r | measure -s Length` in Windows PowerShell.
19 |
20 | Using ReactN will increase your development disc usage by 0.2%.
21 | Using ReactN over Redux will decrease your development disc usage by 0.2%.
22 |
23 | Using Redux will increase your development disc usage by 0.5%.
24 | Using Redux over ReactN will increase your development disc usage by 0.2%.
25 |
26 | ### Development Memory Size
27 |
28 | The development memory size is how much memory is required to run a development build of your application.
29 |
30 | | Application | Code | Strings | JS Arrays | Typed Arrays | System Objects | Total (KB) | Total (MB) | Total Diff. |
31 | | ----------- | -------- | -------- | --------- | ------------ | -------------- | ---------- | ---------- | --------------- |
32 | | reactn | 1,116 KB | 1,745 KB | 262 KB | 2 KB | 1,904 KB | 15,056 KB | 14.7 MB | -452 KB (-2.9%) |
33 | | redux | 1,140 KB | 1,882 KB | 270 KB | 1 KB | 2,308 KB | 15,508 KB | 15.1 MB | +452 KB (+3.0%) |
34 |
35 | Comparisons were determined using the Memory development tool built into Chrome 68.0.3440.75 (Official Build) (64-bit) after running the application using `yarn start`.
36 |
37 | Using ReactN over Redux will decrease your additional development memory by 2.9%.
38 | Using Redux over ReactN will increase your additional development memory by 3.0%.
39 |
40 | ### Production Memory Size
41 |
42 | The production memory size is how much memory is required to run a production build of your application.
43 |
44 | | Application | Code | Strings | JS Arrays | Typed Arrays | System Objects | Total (KB) | Total (MB) | Total Diff. |
45 | | ----------- | ------ | -------- | --------- | ------------ | -------------- | ---------- | ---------- | --------------- |
46 | | reactn | 366 KB | 176 KB | 13 KB | 1 KB | 1,121 KB | 3,161 KB | 3.1 MB | -199 KB (-5.9%) |
47 | | redux | 389 KB | 183 KB | 13 KB | 1 KB | 1,211 KB | 3,360 KB | 3.3 MB | +199 KB (+6.3%) |
48 |
49 | Comparisons were determined using the Memory development tool built into Chrome 68.0.3440.75 (Official Build) (64-bit) after building the application using `yarn build`.
50 |
51 | Converting from Redux to ReactN will decrease your additional production memory by 5.9%.
52 |
53 | Converting from ReactN to Redux will increase your padditional roduction memory by 6.3%.
54 |
55 | ## Application Testing
56 |
57 | ### Development Memory Size
58 |
59 | The development memory size is how much memory is required to run a development build of your application.
60 |
61 | | Application | Code | Strings | JS Arrays | Typed Arrays | System Objects | Total (KB) | Total (MB) | Total Diff. |
62 | | ----------- | -------- | -------- | --------- | ------------ | -------------- | ---------- | ---------- | --------------- |
63 | | reactn | X KB | X KB | X KB | X KB | X KB | X KB | X MB | -X KB (-X.X%) |
64 | | redux | X KB | X KB | X KB | X KB | X KB | X KB | X MB | +X KB (+X.X%) |
65 |
66 | Comparisons were determined using the Memory development tool built into Chrome 68.0.3440.75 (Official Build) (64-bit) after running the application using `yarn start`.
67 |
68 | Using ReactN over Redux will decrease your development memory by X.X%.
69 | Using Redux over ReactN will increase your development memory by X.X%.
70 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ReactN vs Redux
2 |
3 | This repository contains a test that compares an implementation of the same application using both ReactN and Redux. The resulting applications are compared in size, speed, and memory usage.
4 |
5 | ## Foundation
6 |
7 | The applications are bootstrapped using `create-react-app@2.1.0`, then upgraded to `react@16.7.0-alpha.0` and `react-dom@16.7.0-alpha.0`.
8 |
9 | ### reactn
10 |
11 | The `reactn` application installs the `reactn@0.0.9` dependency.
12 |
13 | ### redux
14 |
15 | The `redux` application installs the `react-redux@5.1.0`, `redux@4.0.1`, and `redux-thunk@2.3.0` dependencies.
16 |
17 | ## Foundation Testing
18 |
19 | Before the implementation even occurs, these are the comparisons of merely the foundations on which the implementations are based.
20 |
21 | An empty global state is initialized and the `App` component is connected to the global state.
22 |
23 | ### Methodology: reactn
24 |
25 | The `App.js` file was adjusted to import from `reactn` instead of `react`.
26 |
27 | ### Methodology: redux
28 |
29 | The `index.js` file was adjusted to create a store and implement the Provider HOC as follows:
30 |
31 | ```JavaScript
32 | import { Provider } from 'react-redux';
33 | import { applyMiddleware, combineReducers, createStore } from 'redux';
34 | import thunk from 'redux-thunk';
35 |
36 | const store = createStore(
37 | combineReducers({ }),
38 | applyMiddleware(thunk)
39 | );
40 | ```
41 |
42 | The `` component was then wrapped in ``.
43 |
44 | The `App.js` file was adjusted to wrap the default export in the `connect` HOC.
45 |
46 | ```JavaScript
47 | import { connect } from 'react-redux';
48 |
49 | export default connect(
50 | null,
51 | null
52 | )(App);
53 | ```
54 |
55 | ### File Count
56 |
57 | The file count metric measures how many files are required to exist in your file system to implement one of these global state management packages.
58 |
59 | The baseline, `create-react-app`, has 24,384 files. The following statistics _include_ `create-react-app`'s files.
60 |
61 | | Application | Files | Files Diff. | CRA Diff. |
62 | | ---------------- | ------ | ------------ | ------------ |
63 | | reactn | 24,396 | -213 (-0.9%) | +12 ( 0.0%) |
64 | | redux | 24,609 | +213 (+0.9%) | +225 (+0.9%) |
65 |
66 | Comparisons were determined using `ls -r | measure -s Length` in Windows PowerShell.
67 |
68 | Using ReactN will increase your file count by 0.0%.
69 |
70 | Using Redux will increase your file count by 0.9%.
71 |
72 | Converting from Redux to ReactN will decrease your additional file count by 0.9%.
73 |
74 | Converting from ReactN to Redux will increase your additional file count by 0.9%.
75 |
76 | ### Production Bundle Size
77 |
78 | The production size in bytes measures how many bytes are contained in the production-ready bundle that the end user downloads.
79 |
80 | The baseline, `create-react-app`, is 465,572 bytes. The following statistics _include_ `create-react-app`'s files.
81 |
82 | | Application | Size | Size Diff. | CRA Diff. |
83 | | ----------- | ------- | ---------------- | ----------------- |
84 | | reactn | 534,729 | -76,818 (-12.6%) | + 69,157 (+14.9%) |
85 | | redux | 611,547 | +76,818 (+14.4%) | +145,975 (+31.4%) |
86 |
87 | Comparisons were determined using `ls -r | measure -s Length` in Windows PowerShell after building the application using `yarn build`.
88 |
89 | Using ReactN will increase your production bundle size by 14.9%.
90 | Using ReactN over Redux will decrease your additional production bundle size by 12.6%.
91 |
92 | Using Redux will increase your production bundle size by 31.4%.
93 | Using Redux over ReactN will increase your additional production bundle size by 14.4%.
94 |
95 | ## Application Testing
96 |
97 | The following tests instantiate a store, reducer, render data from the store, and update that data in response to user interaction.
98 |
99 | The store contains an integer `x` and an object `data`.
100 | For `reactn`, these properties are on the global object, as that is a limitation of ReactN.
101 | For `redux`, these properties are on a `demo` object, as that is a limitation of Redux.
102 |
103 | The `App` React Component is updated to fetch data on mount and store the response in the global state.
104 |
105 | The `App` React Component is updated to read from the global state and display the value of `x` on a button.
106 | When the button is clicked, a reducer is used to increment the value of `x` by 1.
107 |
108 | ### Production Memory Size
109 |
110 | The production memory size is how much memory is required to run a production build of your application.
111 |
112 | | Application | Code | Strings | JS Arrays | Typed Arrays | System Objects | Total (KB) | Total (MB) | Total Diff. |
113 | | ----------- | ------ | -------- | --------- | ------------ | -------------- | ---------- | ---------- | -------------- |
114 | | reactn | 392 KB | 177 KB | 12 KB | 0 KB | 1,620 KB | 3,642 KB | 3.6 MB | +24 KB (+0.7%) |
115 | | redux | 397 KB | 184 KB | 13 KB | 0 KB | 1,512 KB | 3,618 KB | 3.5 MB | -24 KB (-0.7%) |
116 |
117 | Comparisons were determined using the Memory development tool built into Chrome 68.0.3440.75 (Official Build) (64-bit) after building the application using `yarn build`.
118 |
119 | Converting from Redux to ReactN will increase your production memory by 0.7%.
120 |
121 | Converting from ReactN to Redux will decrease your production memory by 0.7%.
122 |
123 | ### Production Bundle Size
124 |
125 | The production size in bytes measures how many bytes are contained in the production-ready bundle that the end user downloads.
126 |
127 | | Application | Size w/ .map | Size Diff. w/ .map | Size w/o .map) | Size Diff. w/o .map |
128 | | ----------- | ------------- | ------------------ | -------------- | ------------------- |
129 | | reactn | 531,736 | -80,256 (-13.1%) | 141,142 | -10,512 (-6.9%) |
130 | | redux | 611,992 | +80,256 (+15.1%) | 151,654 | +10,512 (+7.4%) |
131 |
132 | Comparisons were determined using `ls -r | measure -s Length` in Windows PowerShell after building the application using `yarn build` both including and excluding the `.map` files.
133 |
134 | Converting from Redux to ReactN will decrease your production bundle size by 13.1% or 6.9%, depending on if you consider `.map` files as a part of your bundle size.
135 |
136 | Converting from ReactN to Redux will increase your production bundle size by 15.1% or 7.4%, depending on if you consider `.map` files as a part of your bundle size.
137 |
--------------------------------------------------------------------------------
/reactn/.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 |
--------------------------------------------------------------------------------
/reactn/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | 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.
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
--------------------------------------------------------------------------------
/reactn/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "reactn",
3 | "version": "0.1.0",
4 | "homepage": ".",
5 | "private": true,
6 | "dependencies": {
7 | "react": "^16.7.0-alpha.0",
8 | "react-dom": "^16.7.0-alpha.0",
9 | "react-scripts": "2.1.0",
10 | "reactn": "^0.0.9"
11 | },
12 | "scripts": {
13 | "start": "react-scripts start",
14 | "build": "react-scripts build",
15 | "test": "react-scripts test",
16 | "eject": "react-scripts eject"
17 | },
18 | "eslintConfig": {
19 | "extends": "react-app"
20 | },
21 | "browserslist": [
22 | ">0.2%",
23 | "not dead",
24 | "not ie <= 11",
25 | "not op_mini all"
26 | ]
27 | }
28 |
--------------------------------------------------------------------------------
/reactn/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CharlesStover/reactn-vs-redux/082ea23f297cc335901ddc55be12a4bf14150fe3/reactn/public/favicon.ico
--------------------------------------------------------------------------------
/reactn/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
22 | React App
23 |
24 |
25 |
28 |
29 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/reactn/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 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/reactn/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | animation: App-logo-spin infinite 20s linear;
7 | height: 40vmin;
8 | }
9 |
10 | .App-header {
11 | background-color: #282c34;
12 | min-height: 100vh;
13 | display: flex;
14 | flex-direction: column;
15 | align-items: center;
16 | justify-content: center;
17 | font-size: calc(10px + 2vmin);
18 | color: white;
19 | }
20 |
21 | .App-link {
22 | color: #61dafb;
23 | }
24 |
25 | @keyframes App-logo-spin {
26 | from {
27 | transform: rotate(0deg);
28 | }
29 | to {
30 | transform: rotate(360deg);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/reactn/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'reactn';
2 | import './App.css';
3 |
4 | class App extends Component {
5 |
6 | componentDidMount() {
7 | this.global.fetchData();
8 | }
9 |
10 | render() {
11 | return (
12 |
16 | );
17 | }
18 | }
19 |
20 | export default App;
21 |
--------------------------------------------------------------------------------
/reactn/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/reactn/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6 | sans-serif;
7 | -webkit-font-smoothing: antialiased;
8 | -moz-osx-font-smoothing: grayscale;
9 | }
10 |
11 | code {
12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13 | monospace;
14 | }
15 |
--------------------------------------------------------------------------------
/reactn/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { addReducer, setGlobal } from 'reactn';
3 | import ReactDOM from 'react-dom';
4 | import './index.css';
5 | import App from './App';
6 | import * as serviceWorker from './serviceWorker';
7 |
8 | addReducer('fetchData', () =>
9 | fetch('index.html')
10 | .then(response => response.text())
11 | .then(html => ({
12 | data: html
13 | }))
14 | );
15 |
16 | addReducer('incrementX', state => ({
17 | x: state.x + 1
18 | }));
19 |
20 | setGlobal({
21 | data: null,
22 | x: 0
23 | });
24 |
25 | ReactDOM.render(, document.getElementById('root'));
26 |
27 | // If you want your app to work offline and load faster, you can change
28 | // unregister() to register() below. Note this comes with some pitfalls.
29 | // Learn more about service workers: http://bit.ly/CRA-PWA
30 | serviceWorker.unregister();
31 |
--------------------------------------------------------------------------------
/reactn/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/reactn/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 http://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.1/8 is 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 http://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 http://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 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/redux/.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 |
--------------------------------------------------------------------------------
/redux/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | 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.
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
--------------------------------------------------------------------------------
/redux/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "redux",
3 | "version": "0.1.0",
4 | "homepage": ".",
5 | "private": true,
6 | "dependencies": {
7 | "react": "^16.7.0-alpha.0",
8 | "react-dom": "^16.7.0-alpha.0",
9 | "react-redux": "^5.1.0",
10 | "react-scripts": "2.1.0",
11 | "redux": "^4.0.1",
12 | "redux-thunk": "^2.3.0"
13 | },
14 | "scripts": {
15 | "start": "react-scripts start",
16 | "build": "react-scripts build",
17 | "test": "react-scripts test",
18 | "eject": "react-scripts eject"
19 | },
20 | "eslintConfig": {
21 | "extends": "react-app"
22 | },
23 | "browserslist": [
24 | ">0.2%",
25 | "not dead",
26 | "not ie <= 11",
27 | "not op_mini all"
28 | ]
29 | }
30 |
--------------------------------------------------------------------------------
/redux/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/CharlesStover/reactn-vs-redux/082ea23f297cc335901ddc55be12a4bf14150fe3/redux/public/favicon.ico
--------------------------------------------------------------------------------
/redux/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
22 | React App
23 |
24 |
25 |
28 |
29 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/redux/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 | "start_url": ".",
12 | "display": "standalone",
13 | "theme_color": "#000000",
14 | "background_color": "#ffffff"
15 | }
16 |
--------------------------------------------------------------------------------
/redux/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | animation: App-logo-spin infinite 20s linear;
7 | height: 40vmin;
8 | }
9 |
10 | .App-header {
11 | background-color: #282c34;
12 | min-height: 100vh;
13 | display: flex;
14 | flex-direction: column;
15 | align-items: center;
16 | justify-content: center;
17 | font-size: calc(10px + 2vmin);
18 | color: white;
19 | }
20 |
21 | .App-link {
22 | color: #61dafb;
23 | }
24 |
25 | @keyframes App-logo-spin {
26 | from {
27 | transform: rotate(0deg);
28 | }
29 | to {
30 | transform: rotate(360deg);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/redux/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import './App.css';
3 |
4 | import { connect } from 'react-redux';
5 |
6 | class App extends Component {
7 |
8 | componentDidMount() {
9 | this.props.fetchData();
10 | }
11 |
12 | render() {
13 | return (
14 |
18 | );
19 | }
20 | }
21 |
22 | const incrementX = () => ({
23 | type: 'INCREMENT_X'
24 | });
25 |
26 | const receiveData = response => ({
27 | type: 'RECEIVE_DATA',
28 | response
29 | });
30 |
31 | const fetchData = () =>
32 | dispatch => {
33 | fetch('index.html')
34 | .then(response => response.text())
35 | .then(receiveData)
36 | .then(dispatch);
37 | };
38 |
39 | export default connect(
40 | ({ demo }) => ({
41 | data: demo.data,
42 | x: demo.x
43 | }),
44 | ({
45 | fetchData,
46 | incrementX
47 | })
48 | )(App);
49 |
--------------------------------------------------------------------------------
/redux/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | it('renders without crashing', () => {
6 | const div = document.createElement('div');
7 | ReactDOM.render(, div);
8 | ReactDOM.unmountComponentAtNode(div);
9 | });
10 |
--------------------------------------------------------------------------------
/redux/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6 | sans-serif;
7 | -webkit-font-smoothing: antialiased;
8 | -moz-osx-font-smoothing: grayscale;
9 | }
10 |
11 | code {
12 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13 | monospace;
14 | }
15 |
--------------------------------------------------------------------------------
/redux/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 * as serviceWorker from './serviceWorker';
6 |
7 | import { Provider } from 'react-redux';
8 | import { applyMiddleware, combineReducers, createStore } from 'redux';
9 | import thunk from 'redux-thunk';
10 |
11 | const INITIAL_DEMO_STATE = {
12 | data: null,
13 | x: 0
14 | };
15 |
16 | const demoReducer = (state = INITIAL_DEMO_STATE, action) => {
17 | switch (action.type) {
18 |
19 | case 'RECEIVE_DATA':
20 | return {
21 | ...state,
22 | data: action.response
23 | };
24 |
25 | case 'INCREMENT_X':
26 | return {
27 | ...state,
28 | x: state.x + 1
29 | };
30 |
31 | default:
32 | return state;
33 | }
34 | };
35 |
36 | const store = createStore(
37 | combineReducers({ demo: demoReducer }),
38 | applyMiddleware(thunk)
39 | );
40 |
41 | ReactDOM.render(
42 |
43 |
44 | ,
45 | document.getElementById('root')
46 | );
47 |
48 | // If you want your app to work offline and load faster, you can change
49 | // unregister() to register() below. Note this comes with some pitfalls.
50 | // Learn more about service workers: http://bit.ly/CRA-PWA
51 | serviceWorker.unregister();
52 |
--------------------------------------------------------------------------------
/redux/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/redux/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 http://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.1/8 is 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 http://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 http://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 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------