如何在react -native中更改/更新redux中存储的值 [英] How to change/update value in store in redux in react -native

查看:80
本文介绍了如何在react -native中更改/更新redux中存储的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 redux 在 redux 中存储用户数据,例如他的姓名、用户 ID、用户图像和用户电子邮件.减速器是

i am using redux to store the user data like his name,user id ,user image and user email in redux. The reducer for that is

const initialState = {
  user: []
};
function rootReducer(state = initialState, action) {

  if (action.type === 'ADD_USER') {
    return Object.assign({}, state, {
      user: state.user.concat(action.payload)
    });

  }
  if (action.type === 'REMOVE_USER') {
    //return Object.assign({}, initialState)
    return {
      ...state,
      user : []
    }

  }
  if (action.type === 'CHANGE_USER') {

     state.user.splice(0)

    return Object.assign({}, state, {
      user: state.user.concat(action.payload)
    });

  }


  return state;
};
export default rootReducer;

添加用户,我是这样调度的

To add user i am dispatching like this

 let user_id = response[0][1]
          let user_name = response[1][1]
          let user_image = response[2][1]
          let email = response[3][1]

            this.props.add({ user_id, user_name, user_image, email });

假设现在我想更改用户的电子邮件或姓名,那么我如何更改/更新特定字段,如电子邮件?目前我正在这样做

Suppose now that i want to change the email or name of the user so how can i change/update the specific field like email ? Currently i am doing like this

this.props.change({ user_id : 1, user_name : 'sdfd', user_image:'', email:'abc@gmail.com' });

这会传递一个动作 CHNAGE_USER在上面一行之后,我打电话给

This passes an action CHNAGE_USER After above line i am calling

let a = JSON.stringify(this.props.user)
  console.log("val-----------",a)

我的意思是在更新数据后,我试图获取最新数据,但它给了我空数组

I mean after updating data i am trying to get latest but it gives me empty array

val----------- []

请告诉我更改整个对象的方法和更改电子邮件等特定字段的方法?提前谢谢!

Please tell me both the way to change whole object and to change specific field like email ? Thanks is advance !

推荐答案

在 redux 文档中,您可以找到 在状态数组中插入/删除/更新项.

In the redux documentation you can find recipes for inserting/removing/updateing item in an state array.

这篇关于如何在react -native中更改/更新redux中存储的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆