๐ redux / moduls / counter.js
// ์ด๊ธฐ ์ํ(state)
const initialState = {
number: 0
}
//const [number, setNumber] = useState(0)
// ๋ฆฌ๋์ : ํจ์
// input : state์ action
const counter = (state=initialState, action) => {
switch (action.type) {
default:
return state
}
}
export default counter
๐ redux / moduls / users.js
// ์ด๊ธฐ ์ํ(state)
const initialState = {
userId: 123
}
// input : state์ action
const users = (state=initialState, action) => {
switch (action.type) {
default:
return state
}
}
export default users
๐ redux / config / configStore.js
// ์ค์ ๋ฐ์ดํฐ ๊ด๋ฆฌ์(store)๋ฅผ ์ค์ ํ๋ ๋ถ๋ถ
import { combineReducers, createStore } from "redux";
import counter from "../moduls/counter";
import users from "../moduls/users";
const rootReducer = combineReducers({
counter, users
})
const store = createStore(rootReducer)
export default store
๐ App.jsx
import React from 'react'
import { useSelector } from 'react-redux'
function App() {
// ์ฌ๊ธฐ์์ store์ ์ ๊ทผํ์ฌ, counter์ ๊ฐ์ ์ฝ์ด์ค๊ณ ์ถ๋ค
// useSelector
// ๋งค๊ฐ๋ณ์์ state๋ ์ค์์ ์ฅ์ ์์ ์๋ state ์ ์ฒด๋ฅผ ๋งํ๋ค.
const data = useSelector((state) => {
return state;
})
console.log(data)
return (
<div>App</div>
)
}
export default App
const data = useSelector((state) => {
return state;
})
const counter = useSelector((state) => {
return state.counter;
})
console.log(counter.number)
state
-> state.counter
์ด๋ ๊ฒ ๋ฐ๊พธ๋ฉด ๋จ.