0

The following Simple ReactJS code is not working. Newbie needs help please!

<html lang="en">
<head>
<meta charset="UTF-8">
<title>ReactJS Test</title>
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div id="container"></div>

<script type="text/babel">
var currentLocation = window.location;
ReactDOM.render(
        <div>
            <p>currentLocation : {currentLocation}</p>
        </div>,
    document.getElementById('container')
);
</script>
</body>
</html>

And Chrome console shows helpless error messages like this:

react-dom.production.min.js:67 Uncaught Error: Minified React error #31; visit http://facebook.github.io/react/docs/error-decoder.html?invariant=31&args[]=http%3A%2F%2Fxxxxxxx.com%3A8080%2F_test%2Fsitetest_new.jsp&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at m (react-dom.production.min.js:67)
    at qb (react-dom.production.min.js:82)
    at z (react-dom.production.min.js:87)
    at C (react-dom.production.min.js:89)
    at react-dom.production.min.js:94
    at g (react-dom.production.min.js:43)
    at f (react-dom.production.min.js:43)
    at beginWork (react-dom.production.min.js:48)
    at e (react-dom.production.min.js:18)
    at k (react-dom.production.min.js:19)

Thanks in advance!

2
  • Did you visit the link provided or use a non-production build? Commented Sep 28, 2017 at 1:00
  • Yes, it shows "Objects are not valid as a React child" Commented Sep 28, 2017 at 9:33

1 Answer 1

3

window.location returns an object. You can't embed a standalone object in JSX. You can only embed JavaScript expressions.

Try using JSON.stringify() to convert your object to a string.

You can run the snippet below or check out this CodePen Demo.

var currentLocation = window.location;

ReactDOM.render(
        <div>
            <p>currentLocation : {JSON.stringify(currentLocation)}</p>
        </div>,
    document.getElementById('container')
);
<div id="container"></div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>


Note: If you just want a string value from your window.location object (e.g. href), you don't need JSON.stringify().

You could simply use window.location.href directly in your JSX (or set var currentLocation = window.location.ref;) - example.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem! I'm glad it helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.