[react]redixを使用してカウンタモジュールを作成する
reduceモジュールは、以下のすべての項目を含むJavaScriptファイルを表します.動作タイプ 動作作成関数 李杜瑟 まずreduxプリファレンスパラメータから始めましょう
npx create-react-app redux-project
cd redux-project
yarn add redux
yarn add react-redux
module/counter.js
npx create-react-app redux-project
cd redux-project
yarn add redux
yarn add react-redux
module/counter.js
const SET_DIFF = "counter/SET_DIFF";
const INCREASE = "counter/INCREASE";
const DECREASE = "counter/DECREASE";
export const setDiff = diff => ({type: SET_DIFF, diff})
export const increase = () =>({type:INCREASE})
export const decrease = () =>({type:DECREASE})
const initialState = {
number : 0,
diff : 1
}
/*reducer 선언*/
export default function counter(state=initialState, action){
switch(action.type){
case SET_DIFF:
return{
...state,
diff:action.diff
};
case INCREASE:
return{
...state,
number: state.number + state.diff
}
case DECREASE:
return{
...state,
number: state.number - state.diff
}
default:
return state;
}
}
module/index.jsimport { combineReducers } from "redux";
import counter from './counter';
import todos from "./todos";
const rootReducer = combineReducers({
counter,
});
export default rootReducer;
components/Counter.jsimport React from "react";
function Counter({ number, diff, onIncrease, onDecrease, onSetDiff }) {
const onChange = e => {
// e.target.value 의 타입은 문자열이기 때문에 숫자로 변환해주어야 합니다.
onSetDiff(parseInt(e.target.value, 10));
};
return (
<div>
<h1>{number}</h1>
<div>
<input type="number" value={diff} min="1" onChange={onChange} />
<button onClick={onIncrease}>+</button>
<button onClick={onDecrease}>-</button>
</div>
</div>
);
}
export default Counter;
containers/CounterContainer.jsimport React from "react";
import { useSelector, useDispatch } from "react-redux";
import Counter from "../components/Counter";
import { increase, decrease, setDiff } from "../modules/counter";
function CounterContainer() {
const { number, diff } = useSelector(state => ({
number: state.counter.number,
diff: state.counter.diff
}));
const dispatch = useDispatch();
const onIncrease = () => dispatch(increase());
const onDecrease = () => dispatch(decrease());
const onSetDiff = diff => dispatch(setDiff(diff));
return (
<Counter
// 상태와
number={number}
diff={diff}
// 액션을 디스패치 하는 함수들을 props로 넣어줍니다.
onIncrease={onIncrease}
onDecrease={onDecrease}
onSetDiff={onSetDiff}
/>
);
}
export default CounterContainer;
app.jsimport './App.css';
import CounterContainer from './containers/CounterContainer';
function App() {
return (
<div>
<CounterContainer/>
</div>
);
}
export default App;
index.jsimport React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {createStore} from 'redux';
import rootReducer from './modules';
import {Provider} from 'react-redux';
const store = createStore(rootReducer);
console.log(store.getState());
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
ソース:https://react.vlpt.us/redux/05-counter.htmlReference
この問題について([react]redixを使用してカウンタモジュールを作成する), 我々は、より多くの情報をここで見つけました https://velog.io/@shw779/reactredux로-counter-모듈-만들기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol