【问题标题】:How to store object in redux react (Open Weather API data)如何在 redux react 中存储对象(开放天气 API 数据)
【发布时间】:2021-08-09 15:56:18
【问题描述】:

我想在 redux 中存储一个对象,但它给出了错误--->

未处理的拒绝(错误):对象作为 React 子对象无效(发现:对象带有键 {coord, weather, base, main, visibility, wind, clouds, dt, sys, timezone, id, name, cod} )。如果您打算渲染一组子项,请改用数组。

它正在保存数据但它不呈现它显示上述错误。 你能帮我保存这个对象吗?

代码

dispatch(weather(data)); //storing the object
//action
export const weather = (t) =>{
    return({
        type: 'Change',
        payload: t,
    });
}
//reducers
const weather = (state = null, action) => {
    switch (action.type) {
        case "Change_weather":
            return state = action.payload;
        
        default:
            return state;
    }
}
export default weather;

【问题讨论】:

  • 你能分享你的代码吗?
  • @iskandar47,共享

标签: reactjs react-native redux react-redux


【解决方案1】:

您必须将您的操作类型更改为:

//action
export const weather = (t) =>{
    return({
        type: 'Change_weather',
        payload: t,
    });
}

因为如果你保持你的动作类型Change,那么你的reducer 中就没有case 定义来处理Change 类型。而你店铺的初始值为null

【讨论】:

  • 在创建动作类型时使用CHANGE_WEATHER 而不是Change_weather。这是个好习惯。
猜你喜欢
  • 2015-02-22
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 2020-05-12
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多