简体   繁体   English

类型错误:这是未定义的 - React JS,Javascript

[英]TypeError: this is undefined - React JS, Javascript

I am trying to implement a simple react component which change name on clicking.我正在尝试实现一个简单的反应组件,该组件在单击时更改名称。 I am getting the error Error - TypeError: this is undefined while loading my page which has below react component.我收到错误Error - TypeError: this is undefined while loading my page that has below react component. The error seems to be in the setState line.错误似乎在setState行中。

class Layer5 extends React.Component {
  constructor(props) {
    super(props);

    this.state = {buttonstate: false};

  }

  componentDidMount() {

  }

  componentWillUnmount() {

  }

  handleClick(e) {
    e.preventDefault();

    this.setState({buttonstate: !this.state.buttonstate});      
  }

  render() {
    var icon = this.state.buttonstate ? 'images/image1.png' : 'images/image2.jpg';

    return (
      <div className="layer5">
        <a href="#" onClick={this.handleClick}><img src={icon} ></img></a>
      </div>
    );
  }
}

Either a) create a property lexically closed over the instance ( this ) using an arrow function or b) use .bind . a) 使用箭头函数在实例 ( this ) 上创建一个在词法上关闭的属性,或者 b) 使用.bind

Do one or the other.做一个或另一个。 Either a)要么 a)

class Layer5 extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = e => {
      e.preventDefault();

      this.setState({buttonstate: !this.state.buttonstate});
    };
  }
}

or b)或 b)

render() {
    const icon = this.state.buttonstate ? 'images/image1.png' : 'images/image2.jpg';

    return <div className="layer5">
        <a href="#" onClick={this.handleClick.bind(this)}>
          <img src={icon} >
          </img>
        </a>
      </div>;
  }
}

You need to bind this in the constructor for the handleClick method. 您需要将此绑定到handleClick方法的构造函数中。

constructor(props) {
    super(props);

    this.state = {buttonstate: false};
    this.handleClick = this.handleClick.bind(this)
}
import React from 'react'

class UsernameForm extends React.Component {
constructor(props) {
    super(props)
    this.state = {
        username: ''
    }
    this.onchange=this.onChange.bind(this)
    this.onSubmit=this.onSubmit.bind(this)
}
onChange(e) {
    this.setState({
        username: e.target.value
    })
}

onSubmit(e) {
    e.preventDefault()
    this.props.onSubmit(this.state.username)
}

render() {
    return (
        <div>
            <form onSubmit={this.onSubmit}>
                <input type='text'placeholder='What is your username ?' onChange={this.onChange} />
                <input type='submit' />
            </form>
        </div>
    )
}

} }

export default UsernameForm导出默认用户名表单

I am getting error: this is undefined我收到错误:这是未定义的

So that i bind this on {this.onChange} see the solution below所以我将它绑定在 {this.onChange} 上,请参阅下面的解决方案

import React from 'react'


class UsernameForm extends React.Component {
constructor(props) {
    super(props)
    this.state = {
        username: ''
    }
    this.onchange=this.onChange.bind(this)
    this.onSubmit=this.onSubmit.bind(this)
}

onChange(e) {
    this.setState({
        username: e.target.value
    })
}

onSubmit(e) {
    e.preventDefault()
    this.props.onSubmit(this.state.username)
}

render() {
    return (
        <div>
            <form onSubmit={this.onSubmit}>
                <input type='text'placeholder='What is your username ?' onChange={this.onChange.bind(this)} />
                <input type='submit' />
            </form>
        </div>
    )
}

} }

export default UsernameForm导出默认用户名表单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM