Skip to content

Commit 11e9ecc

Browse files
committed
setup counter with redux
1 parent 776a449 commit 11e9ecc

File tree

9 files changed

+131
-20
lines changed

9 files changed

+131
-20
lines changed

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"@testing-library/user-event": "^7.2.1",
99
"react": "^16.13.1",
1010
"react-dom": "^16.13.1",
11-
"react-scripts": "3.4.1"
11+
"react-redux": "^7.2.0",
12+
"react-scripts": "3.4.1",
13+
"redux": "^4.0.5"
1214
},
1315
"scripts": {
1416
"start": "react-scripts start",

src/App.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React from "react";
2+
import "./App.css";
3+
import Counter from "./Counter";
4+
import { Provider } from "react-redux";
5+
import store from "./store";
46

57
function App() {
68
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
9+
<Provider store={store}>
10+
<div className="App">
11+
<Counter />
12+
</div>
13+
</Provider>
2314
);
2415
}
2516

src/Counter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from "react";
2+
import CounterDisplay from "./CounterDisplay";
3+
import CounterIncrementButton from "./CounterIncrementButton";
4+
5+
function Counter() {
6+
return (
7+
<div>
8+
<CounterDisplay />
9+
<CounterIncrementButton />
10+
</div>
11+
);
12+
}
13+
14+
export default Counter;

src/CounterDisplay.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
4+
class CounterDisplay extends React.Component {
5+
render() {
6+
const { counterValue } = this.props;
7+
return <div>CounterValue is: {counterValue}</div>;
8+
}
9+
}
10+
11+
const mapStateToProp = (state) => {
12+
const value = state.value;
13+
14+
return {
15+
counterValue: value,
16+
};
17+
};
18+
19+
export default connect(mapStateToProp)(CounterDisplay);

src/CounterIncrementButton.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
import { updateCounterValue as updateCounterValueFromAction } from "./actions/counter.action";
4+
5+
function CounterButton({ counterValue, updateCounterValue }) {
6+
return (
7+
<div>
8+
<button onClick={() => updateCounterValue(counterValue + 1)}>+1</button>
9+
</div>
10+
);
11+
}
12+
13+
const mapStateToProps = (state) => {
14+
return {
15+
counterValue: state.value,
16+
};
17+
};
18+
19+
const mapDispatchToProps = {
20+
updateCounterValue: updateCounterValueFromAction,
21+
};
22+
23+
export default connect(mapStateToProps, mapDispatchToProps)(CounterButton);

src/actions/counter.action.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const UPDATE_COUNTER = "UPDATE_COUNTER_VALUE";
2+
export const updateCounterValue = (payload) => {
3+
return {
4+
type: UPDATE_COUNTER,
5+
payload: payload,
6+
};
7+
};

src/store/counter.reducer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { UPDATE_COUNTER } from "../actions/counter.action";
2+
const initialState = {
3+
value: 0,
4+
};
5+
6+
const counterReducer = (state = initialState, action) => {
7+
switch (action.type) {
8+
case UPDATE_COUNTER:
9+
return { ...state, value: action.payload };
10+
default:
11+
return state;
12+
}
13+
};
14+
15+
export default counterReducer;

src/store/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createStore } from "redux";
2+
import counterReducer from "./counter.reducer";
3+
4+
const store = createStore(counterReducer);
5+
6+
export default store;

0 commit comments

Comments
 (0)