0

Given the JavaScript below:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
<script type="text/javascript">
    require.config({
        paths: {
            'react': 'https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min',
            'react-dom': 'https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min'
        }
    });
    require(['react', 'react-dom'], function () {
        var x = React;//Error - React is not defined
    });
</script>

Why am I getting an error in the console React is not defined?

enter image description here

1
  • You need to write like this:: require(['react', 'react-dom'], function (React, ReactDom) { var x = React; // React will be defined now :) }); Commented Jan 9, 2018 at 12:44

1 Answer 1

3

You should declare the modules you're importing:

requirejs(['react', 'react-dom'], function(React, ReactDOM) { })

Here's a working example:https://jsfiddle.net/remarkablemark/mejyoLk6/?utm_source=website&utm_medium=embed&utm_campaign=mejyoLk6

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

1 Comment

It's not a declaration. It's a parameter definition in that case. requirejs passes the modules as parameters to the callback provided in the order they were given. You define the identifier React to be the first parameter passed to the callback.

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.