Sunday 21 May 2017

In React app, render() fails because document.getElementById('root') returns null

I'm fairly sure I'm not the only one with this problem, because I have seen this question asked more than once. However, none of the answers provided so far have helped me solve it.

I have a React app, that fails in render() (in index.js):

render(
    <Provider store={store}>
        <Router
            routes={config.router.routes}
            history={browserHistory}
        />
    </Provider>,
    document.getElementById('root')
);

The last line in the function returns null. In the Chrome developer console, this become clear (I added a console.log to show that the result is null):

root element not found

It's fairly obvious that particular error occurs because render() gets null as the second parameter.

However, for the life of me I can't find out why it returns null. I was happily coding away until at some point last night when it stopped working. My public/index.html looks like this:

<!doctype html>
<html lang="en">
<head>
    <script>
    window.settings = {
        "apiUrl": "https://api.goes.here",
        "imgUrl": "https://api.goes.here"
    };
    </script>
</head>
<body>
    <div id="root"></div>
</body>
</html>

(I've clear the actual api values.)

I don't see anything wrong with this too. Now, given that the problem occurred out of the blue, I'm probably missing something incredibly obvious. For completeness, here are my dependencies:

"devDependencies": {
    "react-scripts": "^0.9.5"
  },
  "dependencies": {
    "bootstrap": "^3.3.7",
    "classnames": "^2.2.5",
    "compression": "^1.5.2",
    "firebase": "^4.0.0",
    "jquery": "^3.2.1",
    "linc-profile-generic-react-redux-routerv3": "^3.4.2",
    "prop-types": "^15.5.8",
    "query-string": "^4.3.2",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-helmet": "^5.0.3",
    "react-redux": "^5.0.4",
    "react-router": ">=3.0.0 <4.0.0",
    "react-spinkit": "^2.1.1",
    "redux": "^3.6.0",
    "redux-form": "^6.7.0",
    "redux-logger": "^3.0.1",
    "redux-thunk": "^2.1.0"
  },

I hope someone can shed some light on this. I've ran out of ideas.

Thanks! Sander



via Sandyman

No comments:

Post a Comment