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.

render is called before componentWillMount

See original GitHub issue

It seems like render is called before componentWillMount.

looking in the code it looks like things are happening in this order:

  1. render hook
  2. componentWillMount hook
  3. node is appended to the DOM
  4. componentDidMount hook

Its not as in react spec, but more importantly calling the hook before everything makes a lot of sense to me. otherwise there is no hook that allows touching the component before the first render.

I tried to play with the code a bit, but I couldn’t figure out how to move the hook to the right place.

mainly this line

    rendered = hook(component, 'render', props, state, context);

is positioned before this

    if (component.base) deepHook(inst, 'componentWillMount');
    setComponentProps(inst, childProps, NO_RENDER, childContext);
    renderComponent(inst, DOM_RENDER);
    if (component.base) deepHook(inst, 'componentDidMount');

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

13reactions
developitcommented, Jul 14, 2016

I think this being fixed probably warrants a party of some kind. Cheers, all! 🍻

1reaction
leonid-bauxycommented, May 22, 2016

Still doesn’t work in 5.0.0-beta.1. Unfortunately, constructor isn’t a real replacement. Many react components/mixins/decorators rely on componentWillMount to be called before render. It’s a lot of trouble to go through each and every one of them and rewrite them from using mixins to extending the component class instead, because there is no other way. You can’t override constructor on the prototype via decorator, or create a mixin that does it. I’ve stumbled upon this bug multiple times. componentWillMount is essentially useless if it’s not called before render.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React render() is being called before componentDidMount()
When a component is mounted it is being inserted into the DOM. This is when a constructor is called. componentWillMount is pretty much ......
Read more >
React Lifecycle Methods Render And ComponentDidMount
componentWillMount () method is the least used lifecycle method and called before any HTML element is rendered. If you want to see then...
Read more >
Pre-Mounting with componentWillMount() · react-indepth
The first true life cycle method called is componentWillMount() . This method is only called one time, which is before the initial render....
Read more >
React.Component
UNSAFE_componentWillMount() is invoked just before mounting occurs. It is called before render() , therefore calling setState() synchronously in this method ...
Read more >
componentDidMount is called before render is completly done
componentWillMount is done. _renderSomething is called. I found out about this today when I ...
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