๋ฏธ๋ค์จ์ด๋ก ํฉ์น๋๋ผ ๋ณ ์ฝ์ง์ ๋ค ํ๋ค๊ฐ ๊ฒฐ๋ก ์ ์ผ๋ก action์ ๋ชจ๋ ํฉ์น๊ณ , ์กฐ๊ฑด ์ฒ๋ฆฌ๋ ๋ชจ๋ reducer์์ ์ฒ๋ฆฌํ๋๋ก ํ๋ค.
react ์ค๊ฐ์์ ๋ฏธ๋ค์จ์ด๋ redux์์ ๋ชจ๋ ์ํ, action๋ค์ด ๊ฑฐ์ณ๊ฐ๋ ์ญํ ์ด๋ผ๊ณ ํ์ จ๋๋ฐ(?) ์ฐ๋ฆฌ๋ slice๋ฅผ ๋๊ฐ์ง ์ฐ๊ณ ์๊ณ , ๋ด๊ฐ ์ฒ๋ฆฌํ๋ ค๋ ๊ฒ์ searchSlice์๋ง ๊ดํ ๊ฒ์ด์ด์ ๋ฏธ๋ค์จ์ด์์ ์์ ํ๋ ค๋ฉด action์ด searchSlice์์ ์จ ๊ฑด์ง, clipSlice์์ ์จ ๊ฑด์ง๋ถํฐ ํ๋จํด์ผํ๋ค.
store๋ฅผ ๋ ๊ฐ ์ฐ๊ฑฐ๋, action.type
์ผ๋ก ํ๋ณํ๊ฑฐ๋, try catch
๋ฌธ์ผ๋ก ์ฒ๋ฆฌํ๊ฑฐ๋...
๋ฐฉ๋ฒ์ ๋ค์ํ์ผ๋ ๊ฒฐ๋ก ์ ์ผ๋ก ๋ฏธ๋ค์จ์ด์ ์ญํ ์ด ๋ง๋์ง ํ์ ๋ ์๊ณ , ๊ถ๊ทน์ ์ผ๋ก ๋ด๊ฐ ์ ๊ตฌํํ์ง ๋ชปํ๋ค ใ
;;
๊ฒฐ๊ตญ Input.jsx
์์๋ ๊น๋ํ๊ฒ update
์ก์
๋ง ์์ฑํด์ฃผ๊ณ , updateHistory
์ก์
๋ด์์ ๋ชจ๋ ์ฒ๋ฆฌํด์ฃผ๋๋ก ์์ฑํ๋ค.
(์ด๋ ๊ฒ ์์ฑํ๋ ๊ฒฐ๊ตญ 10๋ถ๋ง์ ์์ฑํ ์ ์์๋ค. ๋ด ํ๋ฃจ๊ฐ... ๐) ๊ด์ฐฎ์! ์ด๊ฒ ๋ค ์ฑ์ฅ์ ์ผ๋ถ์ง ๋ญ
// store.js
import { createSlice } from '@reduxjs/toolkit';
const historyMax = 5;
const searchSlice = createSlice({
name: 'search',
initialState: [],
reducers: {
updateHistory: (state, action) => {
if (state.includes(action.payload)) {
let temp = [...state];
temp.splice(state.indexOf(action.payload), 1);
return [action.payload, ...temp];
}
// ์ต๋ ๊ฐ์์ด๋ฉด ์ญ์ ํ๊ณ ๊ฐ ๋ฃ์ด์ฃผ๊ธฐ
return (state.length === historyMax)
? [action.payload, ...state.slice(0, state.length-1)]
: [action.payload, ...state];
},
},
});
export const { updateHistory } = searchSlice.actions;
export default searchSlice.reducer;
// Input.jsx
import React, { useState } from 'react'
import { useSelector, useDispatch } from 'react-redux';
import { updateHistory } from '../reducer/searchSlice';
export default function Input() {
const history = useSelector(state => state.searchSlice);
const dispatch = useDispatch();
const [value, setValue] = useState("");
const onChange = (e) => {
setValue(e.target.value);
}
const onEnterDown = (e) => {
if (e.key === "Enter") {
searchFunc();
}
}
function searchFunc() {
dispatch(updateHistory(value));
}
return (
<div>
<input onChange={onChange} onKeyDown={onEnterDown}/>
<div> {history.map((value, i) => (<p key={i}>{i} : {value}</p>))}</div>
</div>
)
}
์ด์ Input ์์
์ ๋งก์ ๋์ ํ์์ด ํ๋ฒ Merge๋ฅผ ์งํํด๋ณด๊ธฐ๋ก ํ๋ค.
์ด ๊ณผ์ ์ด ๋งค์ฐ ๊น๋ค๋ก์ ๋ค. ์์ง ํด๊ฒฐ ๋ชป ํจ.
์์ ํ ํ์ผ์์ ๋์์ ์์
์ ํ๋ confilct๊ฐ ๋ฌ๊ณ , ๋น์ฐํ๊ฑฐ๋ผ๊ณ ์๊ฐ์ ํ์ผ๋ ์ด๋ ๊ฒ ํ๋๋ถํฐ ์ด๊น์ง ๋ด๊ฐ ์์ ํด์ผํ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐ ๋ชปํ๋ค. (Git์ด ์ด๋ ์ ๋ ํด์ฃผ๋ ์ค...)
๋ฉํ ๋ง๊ณผ ๋งค๋์ ๋ ๋ ๋ค ๊ณตํต์ ์ผ๋ก ๋ง์ํ์ ๊ฒ์ ํ ๋๋ก ๋ด๊ฐ ๋๋ ๊ฒ์
์ ์ด์ ์์
๋จ์๋ฅผ ๋๋ ๋ ์ด๋ ๊ฒ ์ง๋ฉด ์๋จ.
๋ฌผ๋ก ๋ฏธ๋ ํ๋ก์ ํธ๊ฐ ๋จ์๊ฐ ์๋ค๋ณด๋ ์ด์ฉ ์ ์๊ธด ํ๋๋ฐ, ์ด๋ด๊ฑฐ๋ฉด ํ๋ช
์ด ์ผ๋จ input ์์
ํ๊ณ , ํ ๋ช
์ header ์์
ํ๊ณ ์๊ณ , ์ด๋ฐ ์์ผ๋ก ์ด๋ ์ ๋ ๋ค๋ฅธ ํ์ผ์ ์์
ํ ๋ค์์, input์ด ๋ค ๋๋๋ฉด ๊ทธ ๋ ๋ค๋ฅธ ์ฌ๋ input ์์
์ ํ ์ ์๊ฒ? ์ด๋ฐ ์์ผ๋ก ๋๋ด์ด๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ๋ค.
๋ธ๋์น ์ด๋ฆ์ ๊ธฐ๋ฅ/์ด๋ฆ ์ผ๋ก ํ๊ณ develop์ผ๋ก ๋์ง ๋ง ๊ฒ.
git rebase
git ์์ ํ๋ฆ