티스토리 뷰
http://www.yes24.com/Product/Goods/78233628?scode=032&OzSrank=1
해당 책의 17번 예제를 부분 편집하여 정리한 카운터 코드입니다.
결과물:
CounterComponent.jsx
1
2
3
4
5
6
7
8
9
10
11
12
|
import React from "react";
const CounterComponent = ({number, onIncrease, onDecrease}) => {
return (<>
<h1>숫자: {number}</h1>
<br/>
<button onClick={onIncrease}>+1</button>
<button onClick={onDecrease}>-1</button>
</>)
}
export default CounterComponent
|
cs |
counter.reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import { createAction, handleActions } from "redux-actions";
const INCRESE = 'counter/INCREASE'
const DECRESE = 'counter/DECREASE'
export const increase = createAction(INCRESE)
export const decrease = createAction(DECRESE)
const initialState = {
number: 0
}
const counterReducer = handleActions(
{
[INCRESE]: (state) => ({number: state.number + 1}),
[DECRESE]: (state) => ({number: state.number - 1})
},
initialState
)
export default counterReducer
|
cs |
root.reducer.js
1
2
3
4
5
6
7
8
9
|
import { combineReducers } from 'redux'
import counterReducer from "../counter/counter.reducer";
const rootReducer = combineReducers({
counterReducer
})
export default rootReducer
|
cs |
CounterContainer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import React, {useCallback} from "react";
import { useSelector , useDispatch } from 'react-redux'
import CounterComponent from "./CounterComponent";
import { increase, decrease } from "./counter.reducer";
const CounterContainer = () => {
const number = useSelector(state => state.counterReducer.number)
const dispatch = useDispatch()
const onIncrease = useCallback(()=> dispatch(increase()), [dispatch])
const onDecrease = useCallback(()=> dispatch(decrease()), [dispatch])
return (
<CounterComponent number={number}
onIncrease={onIncrease}
onDecrease={onDecrease}
/>
)
}
export default CounterContainer
|
cs |
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import React from 'react';
import './App.css';
import '@fortawesome/fontawesome-free/css/all.min.css';
import 'bootstrap-css-only/css/bootstrap.min.css';
import 'mdbreact/dist/css/mdb.css';
import CounterContainer from "./counter/CounterContainer";
const App = () => <div className="App">
<CounterContainer/>
</div>
export default App;
|
cs |
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider} from 'react-redux'
import { createStore } from 'redux'
import * as serviceWorker from './serviceWorker';
import rootReducer from "./reducers";
ReactDOM.render(
<Provider store={createStore(rootReducer)}>
<App />
</Provider>,
document.getElementById('root')
);
serviceWorker.unregister();
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import React, {useReducer} from 'react';
const counterTypes = {INCREASE: 'counter/INCREASE', DECREASE: 'counter/DECREASE'}
const counterIncrease = action => ({type: counterTypes.INCREASE, count: action.count + 1})
const counterDecrease = action => ({type: counterTypes.DECREASE, count: action.count - 1})
const counterReducer = (state, action) => {
switch (action.type) {
case counterTypes.INCREASE: return counterIncrease(action)
case counterTypes.DECREASE: return counterDecrease(action)
default: return state
}
}
const Counter = () => {
const [action, dispatch ] = useReducer(counterReducer, {count: 0})
return <>
<h1>{action.count}</h1>
<div>
<button onClick={()=> dispatch({type: counterTypes.INCREASE, count: action.count})}>+1</button>
<button onClick={()=>dispatch({type: counterTypes.DECREASE, count: action.count})}>-1</button>
</div>
</>
}
export default Counter;
|
cs |
'3. 스크립트' 카테고리의 다른 글
Airport.js (0) | 2020.07.23 |
---|---|
리액트 리덕스 단순한 용어 정리 (0) | 2020.07.22 |
React index.tsx 설정 ※리덕스-썽크(redux-thunk) (0) | 2020.07.17 |
[리액트] Scaling your Redux App with ducks (0) | 2020.07.17 |
리액트 카운터 Counter.tsx App.tsx (클래스 버전) (0) | 2020.07.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JUnit
- SQLAlchemy
- nodejs
- Eclipse
- Oracle
- AWS
- jQuery
- vscode
- Django
- COLAB
- Java
- database
- tensorflow
- Python
- springMVC
- Git
- maven
- JPA
- Algorithm
- docker
- intellij
- ERD
- terms
- Mlearn
- KAFKA
- SpringBoot
- mariadb
- React
- Mongo
- FLASK
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함