[TIL]21.08.07

๋ฐ•์ฃผํ™ยท2021๋…„ 8์›” 7์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
48/104

๐Ÿ‘จโ€๐Ÿ’ป์˜ค๋Š˜ ๊ณต๋ถ€ ํ•œ ๊ฒƒ

Redux

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()
);

๋ฆฌ๋•์Šค๋กœ CRUD

์•„์ง 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>
profile
๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

0๊ฐœ์˜ ๋Œ“๊ธ€