yarn add react-redux @reduxjs/toolkit
// Action Value
const ADD_NUMBER = "ADD_NUMBER";
const MINUS_NUMBER = "MINUS_NUMBER";
// Action Creator
export const addNumber = (payload) => {
return {
type: ADD_NUMBER,
payload,
};
};
export const minusNumber = (payload) => {
return {
type: MINUS_NUMBER,
payload,
};
};
// Initial State(기본 값)
const initialState = {
number: 0,
};
// Reducer
const counter = (state = initialState, action) => {
switch (action.type) {
case ADD_NUMBER:
return {
number: state.number + action.payload,
};
case MINUS_NUMBER:
return {
number: state.number - action.payload,
};
default:
return state;
}
};
// export default reducer
export default counter;
import { createSlice } from "@reduxjs/toolkit";
// 초기 값 지정
const initialState = {
number: 0,
};
const counterSlice = createSlice({
name: "counter", // 이 모듈의 이름 지정
initialState, // 이 모듈의 초기 상태 값
reducers: {
addNumber: (state, action) => {
state.number = state.number + action.payload;
}, // 함수 자체가 이 모듈의 reducer 로직 + Action Creator
minusNumber: (state, action) => {
state.number = state.number - action.payload;
},
},
});
// Action Creator는 컴포넌트에서 사용하기 위해 export 하고
export const { addNumber, minusNumber } = counterSlice.actions;
// reducer 는 configStore에 등록하기 위해 export default
export default counterSlice.reducer;
import { createStore } from "redux";
import { combineReducers } from "redux";
import counter from "../modules/counter";
const rootReducer = combineReducers({
counter,
});
const store = createStore(rootReducer);
export default store;
import { configureStore } from "@reduxjs/toolkit";
// slice.reducer를 import 해온다.
import counter from "../modules/counterSlice";
const store = configureStore({
reducer: { counter: counter },
});
export default store;
※ 모듈(Slice)이 여러개인 경우
import { configureStore } from "@reduxjs/toolkit";
import counter from "../modules/counterSlice";
import todos from "../modules/todosSlice";
const store = configureStore({
reducer: { counter: counter, todos: todos },
});
export default store;
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { Provider } from "react-redux";
import store from "./redux/config/configStore";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Provider store={store}>
<App />
</Provider>
);
import React from "react";
import { useSelector } from "react-redux";
const App = () => {
// Store에 있는 todos 모듈 state 조회하기
const todos = useSelector((state) => state.todos);
// Store에 있는 counter 모듈 state 조회하기
const counter = useSelector((state) => state.counter);
return <div>App</div>;
};
export default App;
yarn add json-server
yarn json-server --watch db.json --port 3001
즉, 웹 통신 = 서버(웹 서버)와 클라이언트(웹 브라우저) 간의 대화를 뜻한다.
출처 : https://www.hostinger.in/tutorials/what-is-a-url