├── src
├── App.css
├── pages
│ ├── Products.css
│ ├── Cart.css
│ ├── Products.js
│ └── Cart.js
├── index.css
├── components
│ ├── MainNavigation.js
│ └── MainNavigation.css
├── App.js
├── store
│ ├── actions.js
│ └── reducers.js
└── index.js
├── public
├── favicon.ico
├── manifest.json
└── index.html
├── .gitignore
├── package.json
└── README.md
/src/App.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/academind/react-redux-vs-context/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/src/pages/Products.css:
--------------------------------------------------------------------------------
1 | .products {
2 | width: 50rem;
3 | max-width: 90%;
4 | margin: 2rem auto;
5 | }
6 |
7 | .products ul {
8 | list-style: none;
9 | margin: 0;
10 | padding: 0;
11 | }
12 |
13 | .products li {
14 | padding: 1rem;
15 | margin: 1rem 0;
16 | border: 1px solid #00179b;
17 | display: flex;
18 | justify-content: space-between;
19 | align-items: center;
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/.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 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | * {
2 | box-sizing: border-box;
3 | }
4 |
5 | body {
6 | margin: 0;
7 | font-family: sans-serif;
8 | }
9 |
10 |
11 | button {
12 | font: inherit;
13 | padding: 0.25rem 0.5rem;
14 | background: #00179b;
15 | color: white;
16 | border: none;
17 | cursor: pointer;
18 | }
19 |
20 | button:focus {
21 | outline: none;
22 | }
23 |
24 | button:hover,
25 | button:active {
26 | background: #df6d02;
27 | }
--------------------------------------------------------------------------------
/src/pages/Cart.css:
--------------------------------------------------------------------------------
1 | .cart {
2 | width: 50rem;
3 | max-width: 90%;
4 | margin: 2rem auto;
5 | }
6 |
7 | .cart p {
8 | text-align: center;
9 | }
10 |
11 | .cart ul {
12 | list-style: none;
13 | margin: 0;
14 | padding: 0;
15 | }
16 |
17 | .cart li {
18 | padding: 1rem;
19 | margin: 1rem 0;
20 | border: 1px solid #00179b;
21 | display: flex;
22 | justify-content: space-between;
23 | align-items: center;
24 | }
25 |
--------------------------------------------------------------------------------
/src/components/MainNavigation.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { NavLink } from 'react-router-dom';
3 |
4 | import './MainNavigation.css';
5 |
6 | const mainNavigation = props => (
7 |
8 |
9 |
10 |
11 | Products
12 |
13 |
14 | Cart ({props.cartItemNumber})
15 |
16 |
17 |
18 |
19 | );
20 |
21 | export default mainNavigation;
22 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import { BrowserRouter, Route, Switch } from 'react-router-dom';
3 |
4 | import ProductsPage from './pages/Products';
5 | import CartPage from './pages/Cart';
6 | import './App.css';
7 |
8 | class App extends Component {
9 | render() {
10 | return (
11 |
12 |
13 |
14 |
15 |
16 |
17 | );
18 | }
19 | }
20 |
21 | export default App;
22 |
--------------------------------------------------------------------------------
/src/store/actions.js:
--------------------------------------------------------------------------------
1 | export const ADD_PRODUCT_TO_CART = 'ADD_PRODUCT_TO_CART';
2 | export const REMOVE_PRODUCT_FROM_CART = 'REMOVE_PRODCUT_FROM_CART';
3 |
4 | export const addProductToCart = product => {
5 | return dispatch => {
6 | setTimeout(() => {
7 | dispatch({
8 | type: ADD_PRODUCT_TO_CART,
9 | payload: product
10 | });
11 | }, 700);
12 | };
13 | };
14 |
15 | export const removeProductFromCart = productId => {
16 | return dispatch => {
17 | setTimeout(() => {
18 | dispatch({
19 | type: REMOVE_PRODUCT_FROM_CART,
20 | payload: productId
21 | });
22 | }, 700);
23 | };
24 | };
25 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "redux-vs-context",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "react": "^16.7.0",
7 | "react-dom": "^16.7.0",
8 | "react-redux": "^6.0.0",
9 | "react-router-dom": "^4.3.1",
10 | "react-scripts": "2.1.3",
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 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import { createStore, applyMiddleware } from 'redux';
4 | import { Provider } from 'react-redux';
5 | import reduxThunk from 'redux-thunk';
6 |
7 | import './index.css';
8 | import App from './App';
9 | import shopReducer from './store/reducers';
10 |
11 | const store = createStore(shopReducer, applyMiddleware(reduxThunk));
12 |
13 | ReactDOM.render(
14 |
15 |
16 | ,
17 | document.getElementById('root')
18 | );
19 |
20 | // If you want your app to work offline and load faster, you can change
21 | // unregister() to register() below. Note this comes with some pitfalls.
22 | // Learn more about service workers: http://bit.ly/CRA-PWA
23 |
--------------------------------------------------------------------------------
/src/components/MainNavigation.css:
--------------------------------------------------------------------------------
1 | .main-navigation {
2 | width: 100%;
3 | height: 4.5rem;
4 | background: #00179b;
5 | }
6 |
7 | .main-navigation nav {
8 | display: flex;
9 | justify-content: center;
10 | align-items: center;
11 | height: 100%;
12 | }
13 |
14 | .main-navigation ul {
15 | list-style: none;
16 | margin: 0;
17 | padding: 0;
18 | display: flex;
19 | height: 100%;
20 | justify-content: center;
21 | align-items: center;
22 | }
23 |
24 | .main-navigation li {
25 | margin: 0 1rem;
26 | }
27 |
28 | .main-navigation a {
29 | display: block;
30 | padding: 0.5rem 1rem;
31 | text-decoration: none;
32 | color: white;
33 | border-radius: 5px;
34 | }
35 |
36 | .main-navigation a:hover,
37 | .main-navigation a:active {
38 | background: white;
39 | color: #00179b;
40 | }
41 |
--------------------------------------------------------------------------------
/src/pages/Products.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import { connect } from 'react-redux';
3 |
4 | import MainNavigation from '../components/MainNavigation';
5 | import { addProductToCart } from '../store/actions';
6 | import './Products.css';
7 |
8 | class ProductsPage extends Component {
9 | render() {
10 | return (
11 |
12 |
13 |
14 |
30 |
31 |
32 | );
33 | }
34 | }
35 |
36 | const mapStateToProps = state => {
37 | return {
38 | products: state.products,
39 | cartItemCount: state.cart.reduce((count, curItem) => {
40 | return count + curItem.quantity;
41 | }, 0)
42 | };
43 | };
44 |
45 | const mapDispatchToProps = dispatch => {
46 | return {
47 | addProductToCart: product => dispatch(addProductToCart(product))
48 | };
49 | };
50 |
51 | export default connect(
52 | mapStateToProps,
53 | mapDispatchToProps
54 | )(ProductsPage);
55 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
10 |
11 |
15 |
16 |
25 | React App
26 |
27 |
28 | You need to enable JavaScript to run this app.
29 |
30 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/store/reducers.js:
--------------------------------------------------------------------------------
1 | import { ADD_PRODUCT_TO_CART, REMOVE_PRODUCT_FROM_CART } from './actions';
2 |
3 | const initialState = {
4 | products: [
5 | { id: 'p1', title: 'Gaming Mouse', price: 29.99 },
6 | { id: 'p2', title: 'Harry Potter 3', price: 9.99 },
7 | { id: 'p3', title: 'Used plastic bottle', price: 0.99 },
8 | { id: 'p4', title: 'Half-dried plant', price: 2.99 }
9 | ],
10 | cart: [],
11 | cartSum: 0
12 | };
13 |
14 | const shopReducer = (state = initialState, action) => {
15 | let updatedCart;
16 | let updatedItemIndex;
17 | switch (action.type) {
18 | case ADD_PRODUCT_TO_CART:
19 | updatedCart = [...state.cart];
20 | updatedItemIndex = updatedCart.findIndex(
21 | item => item.id === action.payload.id
22 | );
23 |
24 | if (updatedItemIndex < 0) {
25 | updatedCart.push({ ...action.payload, quantity: 1 });
26 | } else {
27 | const updatedItem = {
28 | ...updatedCart[updatedItemIndex]
29 | };
30 | updatedItem.quantity++;
31 | updatedCart[updatedItemIndex] = updatedItem;
32 | }
33 | return { ...state, cart: updatedCart };
34 | case REMOVE_PRODUCT_FROM_CART:
35 | updatedCart = [...state.cart];
36 | updatedItemIndex = updatedCart.findIndex(
37 | item => item.id === action.payload
38 | );
39 |
40 | const updatedItem = {
41 | ...updatedCart[updatedItemIndex]
42 | };
43 | updatedItem.quantity--;
44 | if (updatedItem.quantity <= 0) {
45 | updatedCart.splice(updatedItemIndex, 1);
46 | } else {
47 | updatedCart[updatedItemIndex] = updatedItem;
48 | }
49 |
50 | return { ...state, cart: updatedCart };
51 | default:
52 | return state;
53 | }
54 | };
55 |
56 | export default shopReducer;
57 |
--------------------------------------------------------------------------------
/src/pages/Cart.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import { connect } from 'react-redux';
3 |
4 | import MainNavigation from '../components/MainNavigation';
5 | import { removeProductFromCart } from '../store/actions';
6 | import './Cart.css';
7 |
8 | class CartPage extends Component {
9 | render() {
10 | return (
11 |
12 |
13 |
14 | {this.props.cartItems.length <= 0 && No Item in the Cart!
}
15 |
35 |
36 |
37 | );
38 | }
39 | }
40 |
41 | const mapStateToProps = state => {
42 | return {
43 | cartItems: state.cart,
44 | cartItemCount: state.cart.reduce((count, curItem) => {
45 | return count + curItem.quantity;
46 | }, 0)
47 | };
48 | };
49 |
50 | const mapDispatchToProps = dispatch => {
51 | return {
52 | removeProductFromCart: id => dispatch(removeProductFromCart(id))
53 | };
54 | };
55 |
56 | export default connect(
57 | mapStateToProps,
58 | mapDispatchToProps
59 | )(CartPage);
60 |
--------------------------------------------------------------------------------
/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 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `npm run build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------