apf*_*pfz 6 reactjs immutable.js redux
我正在尝试从我的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;
Run Code Online (Sandbox Code Playgroud)
减速器包含以下内容:
case ActionTypes.FAQS_GET_AJAX_RECEIVE:
let response: IFaqsGetResponse = action.payload.response && action.payload.response.response;
return state.with({
loading: false,
items: List(response)
});
Run Code Online (Sandbox Code Playgroud)
但是,这给了我以下错误:
Error: Cannot set on an immutable record.
Run Code Online (Sandbox Code Playgroud)
更新
当我从以下方式更换减速器时:
let initial = new FaqsState();
const faqsReducer: Reducer<FaqsState> = (state = initial, action: AppActions) => {
Run Code Online (Sandbox Code Playgroud)
至:
const faqsReducer: Reducer<FaqsState> = (state = null, action: AppActions) => {
state = new FaqsState();
Run Code Online (Sandbox Code Playgroud)
它似乎有效。当我将初始状态作为参数输入时,为什么它不起作用?