๐๐ป props ์ ์ก ๊ณผ์ ์์ด ๋ชจ๋ ์ปดํฌ๋ํธ๊ฐ state์ ์ ๊ทผํ์ฌ ์ฌ์ฉํ ์ ์๋ค!
๐๐ป ์ํ๊ด๋ฆฌ์ ์ ์ฉ
npm install react-router-dom
npm install redux react-redux
import { BrowserRouter } from 'react-router-dom';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
const reduxState = 100;
function reducer(state = reduxState, action){
return state
}
let store = createStore(reducer)
ReactDOM.render(
<BrowserRouter>
<Provider store={store}>
<Redux />
</Provider>
</BrowserRouter>,
document.getElementById('root')
);
๐๐ป Provider์์ ๋๊ฒจ์ค state๋
<Redux />
๋ฟ ์๋๋ผ ๋ค๋ฅธ ์ฌ๋ฌ๊ฐ์ ์ปดํฌ๋ํธ์์๋ ๊บผ๋ด ์ฌ์ฉํ ์ ์๋ค!
useSelector, useDispatch ๋ฑ ํธํ ํ ์ค๊ฐ ์์ด์ connectํจ์๋ฅผ ๋ง์ด์ฐ์ง ์๋๋ค
import React from 'react';
import {useSelector} from 'react-redux';
function Redux(){
const gotReduxState = useSelector( (state) => state);
return(
<div>
๋ฐ์์จ redux state : {gotReduxState} (useSelector)
</div>
)
}
export default Redux;
import React from 'react';
function Redux(props){
return (
<div>
๋ฐ์์จ redux state : {props.gotReduxState} (connect)
</div>
)
}
function stateToProps(props){
return {
gotReduxState : props
}
}
export default connect(stateToProps)(Redux);
๊ฒฐ๊ณผ๋ ๊ฐ๋ค! (์์ค์ฝ๋)
//index.js
const reduxState = 100;
function reducer(state = reduxState, action){
if (action.type === "plus"){
state++;
return state
}
else{
return state
}
}
let store = createStore(reducer);
//Redux.js
function Redux(){
const gotReduxState = useSelector( (state) => state);
const dispatch = useDispatch();
return(
<div>
<button onClick={()=>{ dispatch( {type:'plus'} ) }}> PLUS </button>
</div>
)
}
export default Redux;
๐ store๋ ํ๋์ฌ์ผ ํ๋ค !!
reducer๊ฐ ์ฌ๋ฌ๊ฐ๋ผ๋ฉด ? combineReducers!
// index.js
const reduxState = 100;
function reducer(state = reduxState, action){
if (action.type === "plus"){
state++;
return state
} else{
return state
}
}
const reduxState2 = 200;
function reducer2(state = reduxState2, action){
if (action.type === "plus"){
state++;
return state
} else{
return state
}
}
let store = createStore(combineReducers({reducer, reducer2}));
//Redux.js
const gotReduxState = useSelector( (state) => state.reducer);
useSelector ์ธ์๋ ์ฝ๋ฐฑํจ์
๊ทธ ์ฝ๋ฐฑํจ์์ ์ธ์๋ก state๊ฐ ์ ๋ถ ํฌํจ๋ ๊ฐ์ฒด, ์ฆ index.js์์ Provider๋ก ๋๊ฒจ์ค store์ reducer์ state๊ฐ์ฒด(๊ฐ)
const gotReduxState = useSelector( (state) => console.log(state));
์ฌ๋ฌ๊ฐ์ reducer๋ฅผ combineํด์ ์ฌ์ฉํ ๊ฒฝ์ฐ ์ฌ์ฉํ state๊ฐ์ฒด๋ฅผ ์ง์ ํด์ฃผ์ด์ผ ํ๋ค!
const gotReduxState = useSelector( (state) => state.reducer);
๐ค useDispatch ๋ Store์ ๊ธฐ๋ฅ์ธ dispatch๋ฅผ ํจ์์์ ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ hook ์ ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก (reducer,์ด๊ธฐ์ํ)๋ฅผ ์ธ์๋ก ๋ฐ๋ ํ ์ค๊ฐ useReducer
const [state, dispatch] = useReducer(reducer, initialState);
์ฌ๋ฌ ์ปดํฌ๋ํธ์ ๊ฑธ์ณ ์ฌ์ฉ๋๊ณ ์์ ๋๋ state๋ผ๋ฉด useState ๋์ ์ฌ์ฉํ ์ ์์.
// Redux.js์์
import child from './Child'
< Child />
//Child.js
import React, {useReducer} from 'react';
const initialState = {
inputs: {
value: 'InitialStateValue',
}
}
function reducer(state, action) {
if (action.type === 'change'){
return {
...state,
inputs: {
...state.inputs,
value: action.payload
}
};
}
return state;
}
function UseReducer(){
const [state, dispatch] = useReducer(reducer, initialState);
return(
<div>
UseReducer.js์์ {state.inputs.value}
<button onClick={()=>{ dispatch( {type:'change', payload:'NewStateValue'} ) }}> CHANGE </button>
</div>
)
}
export default UseReducer;
< ContetAPI์ dispatch๋ฅผ ํจ๊ป ์ฌ์ฉํ๋ ์์ >
export const UserDispatch = React.createContext(null);
createContext์ ํ๋ผ๋ฏธํฐ๋ Context์ ๊ธฐ๋ณธ๊ฐ์ด๋ค.
const [state, dispatch] = useReducer(reducer, initialState);
...
return(
<UserDispatch.Provider value={dispatch}>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onToggle={onToggle} onRemove={onRemove} />
</UserDispatch.Provider>
<UserList >์์ ๋ค์๊ณผ ๊ฐ์ด <User >๊ฐ ์๋ค๋ฉด,
return (
<div>
{users.map(user => (
<User user={user} key={user.id} />
))}
</div>
);
<User > ์์๋ dispatch์ ์ ๊ทผํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
const dispatch = useContext(UserDispatch);
return (
<div>
<b
onClick={() => {
dispatch({ type: 'TOGGLE_USER', id: user.id });
}}
>
...
์ ๋ฌํ๋ ์ปดํฌ๋ํธ์์
export const ์ด๋ฆ = React.createContext();
<์ด๋ฆ.Provider value = {์ ๋ฌํ ๊ฐ}>
<์ ๋ฌ๋ฐ์ ์ปดํฌ๋ํธ />
</์ด๋ฆ.Provider>
์ ๋ฌ๋ฐ์ ์ปดํฌ๋ํธ์์
const ๋ฐ์์์ ์ฅํ ์ด๋ฆ = useContext(์ ๋ฌ ์ด๋ฆ);