question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Immutable.js as state?

See original GitHub issue

Sorry. Question again. I’m trying to use Immutable.js for the state. Here is my makeStore function

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducer from './reducer';
import Immutable from 'immutable';
export default function makeStore(initialState = Immutable.fromJS({})) {
    return createStore(reducer, initialState, applyMiddleware(thunk));
}
and then I use this to 'wrap' the Page component 
Page = withRedux(makeStore, (state) => ({
    foo: state.get('foo'),
    custom: 'custom value',
}), actions)(Page);

got error : state.get is not a function can you suggest a way to make redux work with Immutable.js thank you.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:21 (10 by maintainers)

github_iconTop GitHub Comments

10reactions
kirill-konshincommented, Jul 11, 2017

If you still want to use Immutable then you have to modify your makeStore function. In your sample if initialState will be a plain object, it won’t be converted to Immutable. Function should detect if it’s an instance of immutable, and if not - convert it using Immutable.fromJS.

export default function makeStore(initialState = {}) {
    // I use nasty duck typing, you should find a better way to detect
    if (!!initialState.toJS) initialState = Immutable.fromJS(initialState);
    return createStore(reducer, initialState, applyMiddleware(thunk));
}

I suggest not to use ImmutableJS, it’s useless, you can do everything with just spread operators ... and immutable operations on objects and arrays. It only brings problems and forces you to smear all those .get() methods everywhere in your code, so it will be hard to get rid of it later. Keep in mind, that per Redux’s contract, the store’s state is a plain object, so I can’t guarantee that nothing breaks with Immutable.

2reactions
kirill-konshincommented, May 12, 2017

Honestly I think it’s useless, especially for less experienced devs 😃 subtle performance benefits of write operations does not seem to be a good reason to sacrifice read performance or code readability…

I think that if someone is experiencing huge performance issues with ES immutable operations it just mean that the data processing is poorly architected, and ImmutableJS by itself will not help much. But that’s just my opinion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Immutable.js
Designed to inter-operate with your existing JavaScript, Immutable.js accepts plain JavaScript Arrays and Objects anywhere a method expects a Collection .
Read more >
Understanding Immutable State with Immutable.js and ...
Immutable state means its value cannot be changed once it's created. That's it. You might be a bit concerned by this, after all,...
Read more >
React, Redux, and Immutable.js: Ingredients for Efficient UI
In this Immutable.js tutorial, we will build a simple app using React and Redux ... Function shallowEqual will check the props/state diff only...
Read more >
Using an ImmutableJS Object as the React Component State
I am trying to use ImmutableJS to help maintain state in a React component. I can use it just fine if the state...
Read more >
Using Immutable.JS with React and Redux - Fullstack.io
A guide on when to use Immutable.js and how to use it with Redux. ... an application-wide state that any component can access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found