redux-ui icon indicating copy to clipboard operation
redux-ui copied to clipboard

Should we recalculate UI state when props change?

Open tonyhb opened this issue 9 years ago • 6 comments

If your UI state depends on props:

@ui({
  state: {
    name: (props) => props.somethingFromAync
  }
})

We'll calculate the default UI on mount and never recalculate when the async call fetches.

Should we?

Pros

  1. It alleviates async functionality

Cons 2. You might be overriding explicitly set UI state from a user's interaction. This is bad.

Initial thoughts are that if this is an async request, you probably want to defer rendering the component with UI state until that request has finished.

tonyhb avatar Jan 26 '16 18:01 tonyhb

To chime in on this, after running into this issue and thinking 'why is my UI state not updating?!' and then thinking about it some more. I think I agree with how it is implemented just now. For my use-case that I ran into yesterday, I have dropped redux-ui for that, however as a concept I think the way is it just now actually is the 'better' way of doing this.

rogy avatar Mar 23 '16 10:03 rogy

Thanks for the comment @rogy. Can you explain why you had to drop redux-ui? Would be interesting to see if we can create a solution that works for you

tonyhb avatar Mar 25 '16 21:03 tonyhb

Sure, I was using redux-ui inside a modal window.

The modal window contains a component that presents a list of items than can be selected, or unselected (like a multi select)

The modal window also contains a 'create new' button than takes you to another 'builder modal' which is an external application.

The modal finally contains a save button and a cancel button. Global store of selected items is only updated after hitting save, so all selections/deselections inside the modal are tracked by redux-ui

On complete of the 'builder modal' we recieve via postmessage a 'new item' that gets pushed into the store that powers the first modal. This re-renders the 'item list' with the newly created item, but by default we want to select this (but not at the global store level because save has not been hit)

This is where we had the problem of redux-ui not updating its selections which are based on props on a re-render, only on the init.

rogy avatar Mar 26 '16 09:03 rogy

@tonyhb An option might be to provide another key to the config called nextState, which is called on componentWillUpdate. Then you can recalculate the state with the new props.

The hardest part is determining should the component update.

jshthornton avatar Apr 10 '16 03:04 jshthornton

imo we should add this functionality. I am basing my ui-state on an async requests that i call in componentDidMount passed through as a prop and this is not updating my ui-state. Without this i have to manually update my ui-state after the async call which is very annoying...

heks avatar Apr 18 '16 00:04 heks

My idea is using redux-ui to monitor my login state basic on connecting the redux store at root container. Without recalculation, I have to connect any component. It is annoying

mike623 avatar Jan 03 '17 06:01 mike623