News for hackers. Stuff that matters.
Re:A better way to represent loading state in ReactJS/Redux ... by mpodlasin on Sunday June 18, 2017 @04:39AM (#14579668)
This is actually pretty bad. In redux dev tools you will not see if data is being fetched or failure occured. Also, if you serialize and deserialize state object - say save it in local storage - empty array will be neither fetching nor failure one and strange bugs will occur.
Re:A better way to represent loading state in ReactJS/Redux ... by andreareina on Sunday June 18, 2017 @05:34AM (#14579830)
I'm only passingly familiar with redux (read the tutorial + parts of the docs a few times, never actually used it), where is the problem? As I understand it the status of the request is encoded in the store's state. The (de)serialization can be handled as part of the fetch:

    const fetchPics = () => dispatch => {
      dispatch({ type: PICS_REQUEST });
      const cached = deserialize(localStorage.get('pics'));
      if (cached && cached.length > 0) return dispatch({ type: PICS_SUCCESS, ...});

      return api.get('/pics')
        ...
    }
Re:A better way to represent loading state in ReactJS/Redux ... by alangpierce on Sunday June 18, 2017 @07:34AM (#14580204)
The problem is if you try to serialize and deserialize the entire state tree itself, including the loading/failed state. LOADING_STATE, FAILURE_STATE, and an empty array all convert to the same JSON, so if you take your entire state tree, save to JSON somewhere, then load it back, you won't get what you want.

The ability to take your whole application state, save it somewhere (e.g. localstorage), then load it back later is one of the cool things enabled by redux, and it also makes it easier to implement things like time-travel debugging.

Re:A better way to represent loading state in ReactJS/Redux ... by andreareina on Sunday June 18, 2017 @09:57AM (#14580646)
I can see that. Thanks!
Re:A better way to represent loading state in ReactJS/Redux ... by Zalastax on Sunday June 18, 2017 @04:46AM (#14579686)
Good point! I'm confused by the articles suggestion as it already talks about Flow which has support for string literal types. I.e. undefined | "loading" | "failed" | ActualData
Re:A better way to represent loading state in ReactJS/Redux ... by coldtea on Sunday June 18, 2017 @07:11AM (#14580137)
Just because it has a union type, it doesn't mean it's optimal to use it.