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.

Issue with Immutable.js

See original GitHub issue

I’m trying to configure immutable.js with next-redux-wrapper@2.0.0-beta.4 - I followed your suggestion in the docs, but I’m still having lots of issues. It seems that on the server the serialize function is run and state on the server is immutable. However, on the client deserialize is run and state is a POJO. (I’m also using next-redux-saga).

What I see happening is that on initial load the site is SSR and all the data appears correctly. However, after a few second the component re-renders client side and then I get an error that I cannot get values from the items via .get.

I added some console.log statements and see /// serialize state 3 times on the server followed by /// deserialize state 3 times on the client. Once I see that log in the client, that’s when the page errors out and the data in the component is no longer immutable.

P.S. I tried using fromJS and toJS as the serialize and deserialize functions, but for some reason that didn’t work. Using json-immutable from the readme seems to work, initially.

I’ll share some code snippets:

store.js

export function configureStore (initialState = {}) {
  const store = createStore(
    rootReducer,
    initialState,
    bindMiddleware([sagaMiddleware])
  )

  store.runSagaTask = () => {
    store.sagaTask = sagaMiddleware.run(rootSaga)
  };

  // run the rootSaga initially
  store.runSagaTask();
  return store;
}

_app.js

import withRedux from 'next-redux-wrapper';
import nextReduxSaga from 'next-redux-saga';
import { configureStore } from 'store';
const { serialize, deserialize } = require('json-immutable');

...

export default withRedux(configureStore, {
  serializeState: state => {
    console.log('/// serialize state');
    return serialize(state);
  },
  deserializeState: state => {
    console.log('/// deserialize state');
    return deserialize(state)
  }
})(nextReduxSaga(MyApp));

pages/index.js

export class Homepage extends React.Component {
  static async getInitialProps({ store, isServer }) {
    const today = moment().format('MM-DD');
    const items = getItems(store.getState());

    if (!items.size) {
      store.dispatch(requestItems(today));
    }

    return { items };
  }

  render () {
    const { items } = this.props;
    items.map(t => {
      console.log(Immutable.Iterable.isIterable(t));
      return t;
    })
    return (
      <h1>test</h1>
    )
  }
}

export default connect(createStructuredSelector({
  items: getItems,
}))(Homepage);

Thanks for any help. Lmk if you need more context.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kirill-konshincommented, Jun 5, 2018

Done.

FYI serialization is always a conversion of a complex object into something simple.

Wiki: In computer science, in the context of data storage, serialization is the process of translating data structures or object state into a format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across a network connection link) and reconstructed later (possibly in a different computer environment).

2reactions
jasonleibowitzcommented, Jun 5, 2018

My initial assumption was that you serialize JSON into the more complex immutable, and deserialize immutable into JSON.

If people are confused by this my recommendation would be to state this a bit more explicitly in that part of the docs or maybe even an example with Immutable:

export default withRedux(makeStore, {
  serializeState: state => state.toJS(),
  deserializeState: state => fromJS(state),
})(MyApp);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · immutable-js/immutable-js - GitHub
Immutable persistent data collections for Javascript which increase efficiency and simplicity. - Issues · immutable-js/immutable-js.
Read more >
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 Immutability in JavaScript - CSS-Tricks
Immutable data cannot change its structure or the data in it. It's setting a value on a variable that cannot change, making that...
Read more >
React, Redux, and Immutable.js: Ingredients for Efficient UI
Immutable.js allows us to detect changes in JavaScript objects/arrays without resorting to the inefficiencies of deep equality checks, which in turn allows ...
Read more >
Immutability in JavaScript — When and Why Should You Use It
In JavaScript, string and numbers are immutable data types. If that feels strange, here is an example to demonstrate why we call them...
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