티스토리 뷰

3. 스크립트

[Redux] Counter.jsx 7740_40

패스트코드블로그 2020. 7. 21. 18:13

 

http://www.yes24.com/Product/Goods/78233628?scode=032&OzSrank=1

 

리액트를 다루는 기술

리액트 베스트셀러 1위, 본문과 소스 전면 업그레이드기본기를 꼼꼼하게! 실전에서 효과적으로 활용하는 방법까지 알차게 배우자『리액트를 다루는 기술』(개정판)은 리액트 16.8 버전에 Hooks라�

www.yes24.com

해당 책의 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)
        defaultreturn 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

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함