๐จโ๐ป์ค๋ ๊ณต๋ถ ํ ๊ฒ
Store === ์ํ
store์์๋ state๊ฐ ์๋ค. state๋ ์ง์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค.
store์์ reducer๊ฐ ์๋ค
function reducer(oldState, action){
// ...
}
var store = Redux.createStore(reducer);
render๋ redux์ ์๊ด์์ด ์ฐ๋ฆฌ๊ฐ ์งค ui๋ฅผ ๋ง๋ค์ด์ฃผ๋ ์ฝ๋
์ํ์ฐฝ๊ณ ์ง์๋ถ๋ค
dispatch, subscibe, getState(state๊ฐ์ ๊ฐ์ ธ์ค๋)
getState๋ก Store์์ ์๋ state๊ฐ์ ๊ฐ์ ธ์ค๊ณ render์ ์ฌ์ฉํด์ UI๋ฅผ ๋ง๋๋ ๊ฒ (๋ ๋๋ง)
subscribe์ state๊ฐ์ด ๋ฐ๋๋๋ง๋ค render์์ getState๋ก state๊ฐ์ ๋ฒ๊ฑฐ๋กญ๊ฒ ๋ค์ ๊ฐ์ ธ์ค์ง ์๊ณ , ์๋์ผ๋ก state๊ฐ์ ๋ฐ๊ฟ์ ์ฌ๋ ๋๋งํ๊ฒ ํด์ฃผ๋
์ด๋ฐ ์์ผ๋ก ์ฌ์ฉ==>> store.subscribe(render);
dispatch๋ reducer๋ฅผ ํธ์ถํด์ state๊ฐ์ ๊ฐฑ์ ํ๊ณ , subscribe์ ํธ์ถํด์ renderํจ์๋ฅผ ํธ์ถํด์ค
์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํจ==>>
์ฌ๊ธฐ์ dispatch์ ์ธ์๋ก ๋ค์ด๊ฐ ๊ฒ์ด action์ด๋ค.
๊ทธ๋ฆฌ๊ณ ๋ํ reducer๋ ์ด๋ ๊ฒ ์ฌ์ฉํ๋ค.
===>>>
function reducer(state, action){
if(action.type === 'create'){
var newContents = oldState.contents.concat();
var newMaxId = oldState.maxId+1;
newContents.push({id: newMaxId, title: action.payload.title});
return Object.assign({}, state, {
contents: newContents,
maxId: newMaxId,
mode: 'read,
selectedId: newMaxId
});
}
}
** ๋ฆฌ๋์ค๋ฅผ ์ฐ๋ฉด ์ข์ ์ด์
innerHTML = `` <<== html๊ท๊ฒฉ ํ ์คํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋.
*** ๋ฆฌ๋์ค ์ฐ๋ ๋ฒ
1) ๋ฆฌ๋์ค ์ค์น๋ฐฉ๋ฒ
1. npm install --save redux
2. htmlํ์ผ์์
reducer๋ ์ต์ดํ๋ฒ์ ๋ฌด์กฐ๊ฑด ์คํ๋
reducer์์ return ์ผ๋ก state๊ฐ์ ์๋ก ๊ฐฑ์ ํ ๋ ์๋ก์ด ๋ฐ์ดํฐ์ฃผ์๋ฅผ ๊ฐ๋ ์๋ก์ด state๋ฅผ returnํด์ผํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์๊ฐ์ฌํ์ด๋ผ๋ redux์ ์ข์ ๊ธฐ๋ฅ์ ์ฐ์ง ๋ชปํ๋ค.
Object.assign({}, oldState, {color: 'red'});
function test(){
var store = getState();
document.querySelector('body').innerHTML =
<h1>${store.color}</h1>
;
}
store.subscribe(test);
๋์ปคํ๋ง, ๋ฆฌ๋์ค๋ฅผ ์ฌ์ฉํด์ผํ๋ ์ด์
์ฝ๋๊ฐ๊ฒฐ, ๋ณต์ก์ฑ์ค์ด๋ฌ, ๋์ปคํ๋ง ๊ฐ ํจ์, ์ปดํฌ๋ํธ๊ฐ์ ์ฐ๊ฒฐ์ฑ์ด ์ค์ด๋ค์ด ๋ณต์ก์ฑ์ด ์ค์ด๋ ๋ค
*** ์๊ฐ์ฌํ, ๋ฆฌ๋์ค๋๋ฒ๊น
reduxdevtool
๋ฆฌ๋์ค ๋ฐ๋ธํด์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ฝ๋์ ๋ค์๊ณผ ๊ฐ์ ์ต์ ์ด ํ์ํ๋ค.
var store = Redux.createStore(
reducer,
window.REDUX_DEVTOOLS_EXTENSTION &&
window.REDUX_DEVTOOLS_EXTENSTION()
);
์์ง update๋ ๊ตฌํํ์ง ๋ชปํ๋ค ๋ด์ผ ๋ง์ ๊ตฌํํ ์์ ์ด๋ค.
<!doctype html>
<html>
<head>
<meta charset=โUTF-8โ>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.js"></script>
</head>
<body>
<h1></h1>
<br>
<div id="contents_menu"></div>
<br><br>
<div id="crud_menu"></div>
<br><br><br>
<div id="content"></div>
<script>
function reducer(state, action) {
let newState = {};
if (state === undefined) {
newState = {
maxId: 1,
nowId: 0,
mode: 'READ',
contentsMenu: [
{
id: 0,
title: 'Title',
text: 'text1'
},
{
id: 1,
title: 'Title2',
text: 'text2'
}
]
}
}
if (action.type === 'CHANGE_CONTENT') {
newState = Object.assign({}, state, { nowId: action.id });
} else if (action.type === 'CHANGE_MODE') {
newState = Object.assign({}, state, { mode: action.mode });
} else if(action.type === 'CREATE_PUSH'){
newState = Object.assign({}, state, {maxId: state.maxId + 1});
newState.contentsMenu.push({
id: state.maxId + 1,
title: action.title,
text: action.text,
});
} else if(action.type === 'DELETE'){
let temp = state.contentsMenu.filter((el) => el.title !== action.delete);
newState = Object.assign({}, state);
newState.contentsMenu = temp;
}
return newState;
}
const store = Redux.createStore(
reducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
function makeh1() {
let state = store.getState();
document.querySelector('h1').textContent = state.contentsMenu[state.nowId].title;
}
function makeContentsMenu() {
let state = store.getState();
let i = 0;
document.querySelector('#contents_menu').textContent = '';
while (i < state.contentsMenu.length) {
let makeH3 = document.createElement('h3');
makeH3.textContent = state.contentsMenu[i].title;
makeH3.value = state.contentsMenu[i].id
makeH3.onclick = function () {
store.dispatch({
type: 'CHANGE_CONTENT',
id: makeH3.value
});
read();
}
document.querySelector('#contents_menu').append(makeH3);
i++;
}
}
function makeCRUD() {
document.querySelector('#crud_menu').innerHTML = `
<button value="CREATE">CREATE</button>
<button value="READ">READ</button>
<button value="UPDATE">UPDATE</button>
<button value="DELETE">DELETE</button>
`
}
function read() {
let state = store.getState();
document.querySelector('#content').textContent = state.contentsMenu[state.nowId].text;
}
function create() {
let temp = {};
let inputTitle = document.createElement('input');
let inputText = document.createElement('input');
let buttonSend = document.createElement('button');
inputTitle.onchange = (e) => {
temp.title = e.target.value
};
inputText.onchange = (e) => {
temp.text = e.target.value
};
buttonSend.onclick = () => {
store.dispatch({
type: 'CREATE_PUSH',
title: temp.title,
text: temp.text
});
};
buttonSend.textContent = 'Create!!';
let content = document.querySelector('#content');
content.textContent = '';
content.append(inputTitle);
content.append(inputText);
content.append(buttonSend);
}
function _delete(){
let content = document.querySelector('#content');
content.textContent = '';
let input = document.createElement('input');
let buttonSend = document.createElement('button');
let temp = '';
input.onchange = (e) => {
temp = e.target.value;
}
buttonSend.onclick = () => {
store.dispatch({
type: 'DELETE',
delete: temp
})
}
buttonSend.textContent = 'DELETE !!';
content.append(input);
content.append(buttonSend);
}
function reLoading() {
let state = store.getState();
if (state.mode === 'CREATE') {
create();
} else if (state.mode === 'UPDATE') {
} else if (state.mode === 'DELETE') {
_delete();
} else if (state.mode === 'READ') {
read();
}
}
store.subscribe(makeh1);
store.subscribe(makeContentsMenu);
store.subscribe(reLoading);
makeh1();
makeContentsMenu();
makeCRUD();
</script>
</body>
</html>