LoginSignup
18

More than 5 years have passed since last update.

redux-saga での stateの取り方 備忘録

Last updated at Posted at 2017-03-30

redux-saga での stateの取り方

saga でaction の内容を取るときはtakeを使ってキャッチしてこれる

const action = yield take('GET_HOGE');

ここからaction.typeとかaction.payloadとかを色々使う

やりたいこと 

stateとactionを比較してputしたい

現在のstate情報はactionには入って来ないのでちょっと困った

なので selectを使う
redux-saga 公式 select()

公式のAPIを見るとselectgetState()と一緒だよって公式に書いてある

import { select } from 'redux-saga/effects';

const state = yield select();  // store.getState()と同じ

これで一旦、stateの情報は全て取ってこれる

ここから必要な情報だけ取ってくる

selectors を作成

hoge state を取ってくるselectorを作成

selectors/index.js

export const getHoge = state => state.hoge;

これをimport して select()の引数に与える


import { select } from 'redux-saga/effects';

//selector
import { getHoge } from 'selectors';

const state = yield select(getHoge);

これでHogeのstateが取ってこれた!!以上!

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
18