I am trying to update my redux model from my reducer. The model is an extension of an Immutable Record class. I am trying to update the Record with the set method:
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;
The reducer contains the following:
case ActionTypes.FAQS_GET_AJAX_RECEIVE:
let response: IFaqsGetResponse = action.payload.response && action.payload.response.response;
return state.with({
loading: false,
items: List(response)
});
However this gives me the following error:
Error: Cannot set on an immutable record.
UPDATE
When I change the reducer from:
let initial = new FaqsState();const faqsReducer: Reducer<FaqsState> = (state = initial, action: AppActions) => {
to:
const faqsReducer: Reducer<FaqsState> = (state = null, action: AppActions) => {state = new FaqsState();
it seems to work. Why is it not working when I give the initial state in as an argument?
#reactjs #redux