unistore icon indicating copy to clipboard operation
unistore copied to clipboard

run action while componentDidMount do not trigger props update

Open dev-johnny-gh opened this issue 6 years ago • 1 comments

@connect({
  a: 'a',
}, {
  doSomething: () => {
    return {a: 'b'};
  }
})
export class Example extends Component<IProps, IState> {
  componentDidMount() {
    this.props.doSomething();
  }

  componentWillReceiveProps(nextProps) {
    console.log("nextProps", nextProps);
  }
}

this example shows what i had done.

i triggered an action in componentDidMount, but componentWillReceiveProps can not receive any update.

i look into the code and found out the problem is the wrapper outside my component register a listener while componentDidMount, but its child componet, my component, mounted before the wrapper.

so when my component run componentDidMount, the wrapper haven't run the subscription method yet. therefore it can't reveive any update.

so why not use the componentWillMount to replace the componentDidMount. componentWillMount will be triggered in a right order from parent to child. so subscription function can capture and update any changes from the child component.

dev-johnny-gh avatar Aug 09 '19 13:08 dev-johnny-gh

Is this in React, or Preact?

It seems like a bug, due to the reversed execution order for componentDidMount(), which causes the child component's method to fire before the parent (wrapper)'s. The issue stems from here:

https://github.com/developit/unistore/blob/master/src/integrations/react.js#L46

One possible solution would be to use a different heuristic for differentiating between SSR and client-side rendering.

developit avatar Oct 17 '19 20:10 developit