I'm new to the whole reducer and immutable thing, so maybe someone can answer me some questions. I'm using seamless-immutable (https://github.com/rtfeldman/seamless-immutable) along with redux-seammless-immutable (https://github.com/eadmundo/redux-seamless-immutable) in a react native app.
I have some questions for the following code:
import { combineReducers } from 'redux-seamless-immutable';
import Immutable from 'seamless-immutable';
const PRODUCTS_INITIAL_STATE = Immutable({ data: Immutable([]), loading: true });
const productsReducer = (state = PRODUCTS_INITIAL_STATE, action) => {
switch (action.type) {
case "LOADING":
return { ...state, loading: action.payload };
case "LOADED":
return Immutable.merge(state, {data: Immutable(action.payload) });
case "ADD_PRODUCT":
return Immutable.merge(state, {data: Immutable(state.data).concat(action.payload) });
default:
return state;
}
};
const INIT_STORE = Immutable({
products: productsReducer
});
export default combineReducers(INIT_STORE);
- Do I have to define every property as Immutable object like in
const PRODUCTS_INITIAL_STATE = Immutable({ data: Immutable([]), loading: true });or does Immutable take care of that, and it's enough to do aconst PRODUCTS_INITIAL_STATE = Immutable({ data: [], loading: true });? - Am I right, that the JSX approach using three dots
{ ...state, loading: action.payload }is the same as usingImmutable.merge? - Is there an easier way for the
ADD_PRODUCTaction, to add an element to an array that is nested within an object?
Update
Another questions rised: I have an immutable array, that contains objects. When I want to add another element, I have to recreate the array using Immutable(array).concat(newElement). But what if I only want to replace a value inside of one of it's objects?
Consider this array:
const array = Immutable([
{ name: "foo" },
{ name: "bar" }
]);
What's the immutable way to go, if I want to change the name of the first element from foo to hello?
I also new to immutable. But this is my seamless-immutable experience after reading my friend source code.
Yeah,
Immutable({ data: [], loading: true });is just enough. No need nest an immutable object inside immutable.{ ...state, loading: action.payload }andstate.merge({ loading: action.payload })is the same, but Immutable.merge will return Immutable object. You can useisImmutablefunction of seamless-immutable to check whether object is immutable or not.Something likes this, right?
Simply, just use
setapiDo you have any another questions?