1
1

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 1 year has passed since last update.

Redux Sagaのgenerate関数をuseEffectで呼びたい

Posted at

はじめに

Reduxで使っていた関数をコンポーネント側で利用したかったのですが、時間がかかったので書いておきます

問題

以下のような関数があるとします

saga.ts
export function* fetchUsers() {
  console.log("Helllo")
}

この関数をuseEffectの中で呼びたいと思い以下のように書きました

useEffect(() => {
  call(fetchUsers)
}

しかし、useEffectは動いているのにfetchUsersは動いていませんでした

解決方法

Redux Sagaではジェネレーター関数はdispatchで呼ぶしかなさそうでした

ということでアクションを作成して呼び出すことでうまく行きました

useEffect(() => {
  dispatch(fetchUsers())
}

おわりに

Reduxは初心者にとってはなかなか複雑だなと思いました
引き続き頑張りたいです

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?