react componentDidMount not firing

JavascriptReactjs

Javascript Problem Overview


I set up a new react project and for some reason, the componentDidMount method is not being called.

I have verified this behavior by placing a call to console.log in componentDidMount but I cannot see its output in the console.

Furthermore, this.setState() is not working.

I'm pretty stumped as to why the componentDidMount is not being called. I tried using both React "v0.14.0" and "v0.14.3".

Why is 'componentDidMount' not being called?

Code:

var React = require('react');

var RecipePage = React.createClass({
  componentDidMount: function() {
    console.log('mounted!');
  },
  render: function() {
    return (
      <div>Random Page</div>
    );
  }
});

module.exports = RecipePage;

Javascript Solutions


Solution 1 - Javascript

This happened to me when I had componentWillMount() defined 2x in the same class. This did not produce a runtime error. I simply removed the second definition and things started working.

Solution 2 - Javascript

So... lo and behold.... I finally got it working(with the help of a back-end expert). The reason why the "componentDidMount" methods weren't firing was because my server was bundling and serving all the react + html stuff on server side. (Only "render" and "getInitialState" methods get called to create the "html" template that gets delivered through the client's browser...but it stops there because it thinks it's finished)

The fix: Find a way to deliver the resulting "compiled" html through the server AND in addition, allow react's own events to be accessible and "fireable" again on the client side. When compiling my "view" file( index.js or index.html ), I included an "Application.start()" script that injects my bundle.js code into the template again. Then in my gulpfile, exported the "Application" variable so the "view" file can access it.

Gahh...pulled my hair out for this. Time to read up on server side vs. client side rendering

Solution 3 - Javascript

In my case, there was another componentDidMount earlier in the code

Solution 4 - Javascript

I have the create-react-app template. I added the ComponentDidMount into the App Component. I put a console.log and an alert into it. It does not fire and there are no errors in React or the browser. I tried in chrome and firefox.

FIX: what worked for me was rebooting my pc. Then it started working. I do not know why, but this resolve the issue.

Update: Not I have an uppercase 'C' it is 'componentDidMount' not 'ComponentDidMount'.. I think it's time for sleep.

Solution 5 - Javascript

In my case, I call componentDidMount() for retrieving data from a service (API call) and on the render function I have components that are supposed to use the data that is returned from this call.

But because the call is Async the render() function is called immediately and then it crashes (many errors like: "TypeError:cannot read property 'XXXX' of undefined" ) then it looks like componentDidMount is not called at all.

To solve this issue I check on render that myData !== null before returning the component itself - You can use generic loader\spinner on this case that will not render internal components that use data before the data that actually retrieve from service.

Example:

componentDidMount() {
    const { GetMyDataFromServiceFunc } = this.props;

    GetMyDataFromServiceFunc ("150288");
}

render() {
    const { details, loading } = this.props;
    
    if (!details)
        return (<GenericLoader />);
        
        return (

        <FullScreenModal
            <Wizard className="order-new-wizard">

                <Component1 editable={false} dataToComponent1={details}
                    goNextStep={this.goNextStep} /> 

                <Component2 values={details.customerDetail} goNextStep={this.goNextStep} />
            </Wizard>
        </FullScreenModal>
        );
    }
}

Solution 6 - Javascript

You need to change your module.exports to point at RecipePage not TestPage

module.exports = RecipePage;

Solution 7 - Javascript

For me, the reason was upper case "C" in ComponentDidMount.

Changed it to componentDidMount and it worked

Solution 8 - Javascript

For me I also declared componentDidUpdate in the class after that componentDidMount started firing as expected.

Solution 9 - Javascript

In my case, I imported a component without the export default command in the imported file. When I fixed this, componentDidMount started firing...

Solution 10 - Javascript

For me, I had onComponentDidMount() instead of componentDidMount() facepalm

Solution 11 - Javascript

Although you have a console log statement in your componentDidMount function it may not be executing because that function never gets run. If the error occurs before componentDidMount gets called such as in your render function, you won't see the console log. Try commenting out the code in your render function and see if it appears.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3295436View Question on Stackoverflow
Solution 1 - JavascriptP.Brian.MackeyView Answer on Stackoverflow
Solution 2 - Javascriptuser3295436View Answer on Stackoverflow
Solution 3 - Javascriptuser5480949View Answer on Stackoverflow
Solution 4 - JavascriptSigexView Answer on Stackoverflow
Solution 5 - JavascriptYohanView Answer on Stackoverflow
Solution 6 - Javascriptyangli-ioView Answer on Stackoverflow
Solution 7 - JavascriptAnkur MarwahaView Answer on Stackoverflow
Solution 8 - JavascriptShounak BoseView Answer on Stackoverflow
Solution 9 - JavascriptKing James EnejoView Answer on Stackoverflow
Solution 10 - JavascriptMarloView Answer on Stackoverflow
Solution 11 - JavascriptnflauriaView Answer on Stackoverflow