0

I did a html page like this

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>React project</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]/browser.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script src="react.js" type="text/babel"></script>
  </body>
</html>

And the file react.js like this

var Comp1 = React.createClass({
  render: function() {
    return(
      <h1> Comp1 </h1>
    );
  }
});


ReactDOM.render(
  <div>
    <Comp1/>
  </div>,
  document.getElementById("root")
);

But, I got "Uncaught TypeError: React.createClass is not a function" exception, why?

1

1 Answer 1

2

Normally you would define a React component as a plain JavaScript class:

class Comp1 extends React.Component {
  render() {
    return <h1> Comp1 </h1>;
  }
}

If you don’t use ES6 yet, you may use the create-react-class module instead:

var createReactClass = require('create-react-class');
var Comp1= createReactClass({
  render: function() {
    return <h1> Comp1 </h1>;
  }
});

Or,

import React from 'react';

const Contacts = React.createClass({
  render() {
    return (
      <div></div>
    );
  }
});

export default Contacts;
Sign up to request clarification or add additional context in comments.

Comments

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.