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

【備忘録】react-reduxでオブジェクトのstateを更新する

Posted at

stateの構造

state = {
  user: {
    name: string,
    address: string,
    phoneNumber: string
  },
  isPremiun: boolean
}

reducerの記述

initialState = {
  user: {
    name: "",
    address: "",
    phoneNumber: ""
  },
  isPremiun: false
}
const userReducer = (state = initialState, action) => {
  const { type, payload } = action;
  switch (type) {
    case user/updateProfile:
      return {...state, user: {...state.user, ...payload}};
    default:
      return state;
  }
}

このようにしておくと、userの情報を更新したいときにname, address, phoneNumberの情報全てを送信する必要がない。
{name: "hoge"}という情報をactionのpayloadに渡すとその部分だけ更新される。

0
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
0
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?