Reactjs, Super expression must either be null or a function

后端 未结 2 1760
夕颜
夕颜 2020-12-22 02:32

I use the React to write this demo. I use the Webpack to build this demo.When I start this demo, the error will show me.

The error:

Uncaught

相关标签:
2条回答
  • 2020-12-22 03:19

    The only warning in your code is due the the fact that you are not extending the correct class, you need to extend React.Component.

    class App extends React.Component {
            constructor(props){
                super(props);
                this.handleClick = this.handleClick.bind(this);
            }
            handleClick(){
                if(this.myTextInput !=null) {
                    this.myTextInput.focus();
                }
            }
            render (){
                return (
                    <div>
                        <input type="text" ref={(ref) => this.myTextInput = ref} />
                        <input type="button"
                            value="'Focus the text input"
                               onClick={this.handleClick}
                        />
                    </div>
                );
            }
        }
        ReactDOM.render(<App />, document.getElementById('app'));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.min.js"></script>
    <div id="app"></div>

    0 讨论(0)
  • 2020-12-22 03:25

    try following:

            import React, {Component} from 'react';
            import ReactDOM from 'react-dom';
            class App extends React.Compoment {
              constructor(props){
                super(props);
                this.myTextInput = this.myTextInput.bind(this);
                this.handleClick = this.handleClick.bind(this);
              }
              handleClick(){
                if(this.myTextInput !=null) {
                  this.myTextInput.focus();
                }
              }
              render (){
                return (
                  <div>
                    <input type="text" ref={(ref) => this.myTextInput = ref} />
                    <input type="button"
                      value="'Focus the text input"
                      onClick={this.handleClick}
                    />
                  </div>
                );
              }
            }
            ReactDOM.render(<App />, document.getElementById('app'));
    
    0 讨论(0)
提交回复
热议问题