加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React redux:无法设置不可变记录

发布时间:2020-12-15 20:20:35 所属栏目:百科 来源:网络整理
导读:我正在尝试从我的reducer更新我的redux模型.该模型是Immutable Record类的扩展.我试图用set方法更新Record: import { List,Record } from 'immutable';import { IFaqItem } from './api.models';export interface IFaqsState { loading?: boolean; items?:
我正在尝试从我的reducer更新我的redux模型.该模型是Immutable Record类的扩展.我试图用set方法更新Record:

import { List,Record } from 'immutable';
import { IFaqItem } from './api.models';

export interface IFaqsState {
  loading?: boolean;
  items?: List<IFaqItem>;
}
const faqsState = Record({
  loading: false,items: List()
});

class FaqsState extends faqsState implements IFaqsState {
  loading: boolean;
  items: List<IFaqItem>;

  with(props: IFaqsState) {
    this.set('loading',props.loading);
    return this;
  }
}

export default FaqsState;

reducer包含以下内容:

case ActionTypes.FAQS_GET_AJAX_RECEIVE:
  let response: IFaqsGetResponse = action.payload.response && action.payload.response.response;
  return state.with({
    loading: false,items: List(response)
  });

但是,这给了我以下错误:

Error: Cannot set on an immutable record.

UPDATE

当我更改减速器时:

let initial = new FaqsState();

const faqsReducer: Reducer<FaqsState> = (state = initial,action: AppActions) => {

至:

const faqsReducer: Reducer<FaqsState> = (state = null,action: AppActions) => {

  state = new FaqsState();

它似乎工作.当我将初始状态作为参数时,为什么它不起作用?

解决方法

嗯,这是不可改变的.当你调用with函数时,你改变了对象,因此违反了它的唯一约束. 为避免这种情况,您应该使用新值创建一个新实例并将其返回到商店.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读