※個人用の記録
※随時、更新
reduxのcreateStoreは何をしているのか
例えば、こんなやつ
import { createStore } from 'redux';
import reducer from './reducer.js';
︙
const store = createStore(reducer);
reducerで作った「引数で受け取ったActionとstateでstateの状態を変更する」役割をreduxのstoreに登録する。
reducerでは何をするのか
reducerの例
const initialState = {
hello: "Hello World",
dio: ""
}
export default function reducer(state = initialState,action) {
switch(action.type) {
case 'HELLO_WORLD':
return {
...state,
hello: action.hello
};
case 'THE_WORLD':
return {
...state,
dio: action.dio
};
default:
return state
}
}
- 第1引数→state
- 第2引数にaction
actionのtypeによって処理が分かれ、↑ではstateのオブジェクトにactionのオブジェクト値を代入する