0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

react-nativeでreduxを使っているときのこと

Last updated at Posted at 2018-07-26

※個人用の記録
※随時、更新

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のオブジェクト値を代入する

参考

createStoreを実装してみたらReduxの理解が捗った

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?