Qiita Conference 2025

こにふぁー (@konifar)

提案のレベルを上げる - 推進力のあるエンジニアになるための思考法

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 3 years have passed since last update.

React + Reduxを基本からまとめてみた【4】【Actions】

Last updated at Posted at 2021-10-05

Fluxフロー図

![flux_flow.png]
(https://camo.qiitausercontent.com/cdd0824a85a48f51dacc8ec20fa5cf5ce6ec8093/68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3434303539332f33313139643830622d383163642d633664382d636233312d6132383837386261313738312e706e67)

Actionsとは

  • 要求を受けて変更を依頼
    ⇨ Actionsを書いてstateの変更を依頼する

Actionsの役割

  • アプリからStoreへデータを送るためのpayload(データの塊)を渡す役割
    ⇨ アプリから受け取ったデータをReducersへ渡す

なぜActionsを使うのか

  • 純粋にデータを渡す処理だけを記述するため
  • どのstateをどのように変更するのかについてはReducersに任せる

Actionsの書き方

  1. Action typeを定義してexport
  2. typeとpayloadを記述する
  3. プレーンなobjectを返す
※Actionsは、プレーンなobjectを返す!!
Sample.js
//1. Action typeを定義してexport
export const SIGN_IN = "SIGN_IN";

export const signInAction = (userState) => {
//3. プレーンなobjectを返す
  return {
//2. typeとpayloadを記述する
     type: "SIGN_IN",
     payload: {
       isSignedIn: true,
       uid: userState.uid,
       username: userState.username
    }
  }
};

参考サイト

[日本一わかりやすいReact-Redux入門#4...Actionsを書いてstateの変更を依頼しよう]
(https://www.youtube.com/watch?v=0JKkLZHmWxk)
[React- Redux 入門]
(https://qiita.com/shtwangy/items/4f9993ade64c3c6858d3)

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?