Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React构造器不工作_Reactjs - Fatal编程技术网

Reactjs React构造器不工作

Reactjs React构造器不工作,reactjs,Reactjs,考虑一下这个类 var sentence = "The FBI has managed to unlock the iPhone"; define(["./UserInput","./Statistic","./RandomWords","react", "react-dom"], (UserInput,Statistic,RandomWords,React, ReactDOM) => { var Root = React.createClass({ c

考虑一下这个类

var sentence = "The FBI has managed to unlock the iPhone";

define(["./UserInput","./Statistic","./RandomWords","react", "react-dom"],
  (UserInput,Statistic,RandomWords,React, ReactDOM) => {

      var Root = React.createClass({
        constructor() {
            super();
            console.log("Hey");
            this.state={sentenct: sentence }

          }

    render: function() {
          return (
            <div >

                <h1>Welcome to our game</h1>
                <UserInput/>
                <RandomWords NewWord={ sentence }/>
                <Statistic   speed={"Salman"}  success={"saba"} />
            </div>
          );
        }
      });

      ReactDOM.render(
        React.createElement(Root, null),
        document.getElementById('content')
      );
});
我试过了,而且 我制作了一个costructor,我希望在我的控制台中看到Hey,但它没有发生?

您必须使用this.state.句子而不是变量

var sentence = "The FBI has managed to unlock the iPhone";

    render: function() {
              return (
                <div >

                    <h1>Welcome to our game</h1>
                    <UserInput/>
                    <RandomWords NewWord={ this.state.sentence }/>
                    <Statistic   speed={"Salman"}  success={"saba"} />
                </div>
              );
    }
如果问题仍然存在,请在下面的注释部分中写下,愿意调查由React.createClass创建的类没有构造函数方法。 只有ES6类具有构造函数方法

例如:

class Root extends React.Component {
    constructor() {
        super();
        console.log("Hey");
        this.state={sentenct: sentence }
    }
}

你为什么不使用ES6呢?
class Root extends React.Component {
    constructor() {
        super();
        console.log("Hey");
        this.state={sentenct: sentence }
    }
}