โ ์ด ๋ถ๋ถ์ Chatbot๊ณผ์ ๋ฉ์ธ์ง ๋ด์ฉ์ Redux์ ์ ์ฅํ๋ React ๋ถ๋ถ์ผ๋ก ํ์์ ์ํด ์๋ต ๊ฐ๋ฅํฉ๋๋ค!
// ๊ธฐ์กด์ Chatbot.js ํ์ผ์ ์ถ๊ฐ
import { useDispatch } from "react-redux";
import { saveMessage } from "../_actions/message_actions";
function Chatbot() {
const dispatch = useDispatch();
...
dispatch(saveMessage(conversation));
...
}
โ๏ธ ./client/.../_reducers ํด๋
// index.js
import { combineReducers } from 'redux';
import message from './message_reducer';
const rootReducer = combineReducers({
message,
});
export default rootReducer;
// message_reducer.js
import { SAVE_MESSAGE } from "../_actions/types";
export default function (state = { messages: [] }, action) {
switch (action.type) {
case SAVE_MESSAGE:
return {
...state,
messages: state.messages.concat(action.payload),
};
default:
return state;
}
}
โ๏ธ ./client/.../_actions ํด๋
// types.js
export const SAVE_MESSAGE = 'save_message';
// message_actions.js
import { SAVE_MESSAGE } from "./types";
export function saveMessage(dataToSubmit) {
return {
type: SAVE_MESSAGE,
payload: dataToSubmit,
};
}
โ ๋ฐ๋ผํ๋ฉฐ ๋ฐฐ์ฐ๋ ๋ ธ๋, ๋ฆฌ์กํธ ์๋ฆฌ์ฆ - ์ฑ๋ด ์ฌ์ดํธ ๋ง๋ค๊ธฐ ๊ฐ์๋ฅผ ์ฐธ๊ณ ํฉ๋๋ค.