React: “this” is undefined inside a component function

前端 未结 10 2114
春和景丽
春和景丽 2020-11-22 04:59
class PlayerControls extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      loopActive: false,
      shuffleActive: false,
    }         


        
相关标签:
10条回答
  • 2020-11-22 05:48

    If you are using babel, you bind 'this' using ES7 bind operator https://babeljs.io/docs/en/babel-plugin-transform-function-bind#auto-self-binding

    export default class SignupPage extends React.Component {
      constructor(props) {
        super(props);
      }
    
      handleSubmit(e) {
        e.preventDefault(); 
    
        const data = { 
          email: this.refs.email.value,
        } 
      }
    
      render() {
    
        const {errors} = this.props;
    
        return (
          <div className="view-container registrations new">
            <main>
              <form id="sign_up_form" onSubmit={::this.handleSubmit}>
                <div className="field">
                  <input ref="email" id="user_email" type="email" placeholder="Email"  />
                </div>
                <div className="field">
                  <input ref="password" id="user_password" type="new-password" placeholder="Password"  />
                </div>
                <button type="submit">Sign up</button>
              </form>
            </main>
          </div>
        )
      }
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:49

    in my case this was the solution = () => {}

    methodName = (params) => {
    //your code here with this.something
    }
    
    0 讨论(0)
  • 2020-11-22 05:51

    You should notice that this depends on how function is invoked ie: when a function is called as a method of an object, its this is set to the object the method is called on.

    this is accessible in JSX context as your component object, so you can call your desired method inline as this method.

    If you just pass reference to function/method, it seems that react will invoke it as independent function.

    onClick={this.onToggleLoop} // Here you just passing reference, React will invoke it as independent function and this will be undefined
    
    onClick={()=>this.onToggleLoop()} // Here you invoking your desired function as method of this, and this in that function will be set to object from that function is called ie: your component object
    
    0 讨论(0)
  • 2020-11-22 05:51

    You can rewrite how your onToggleLoop method is called from your render() method.

    render() {
        var shuffleClassName = this.state.toggleActive ? "player-control-icon active" : "player-control-icon"
    
    return (
      <div className="player-controls">
        <FontAwesome
          className="player-control-icon"
          name='refresh'
          onClick={(event) => this.onToggleLoop(event)}
          spin={this.state.loopActive}
        />       
      </div>
        );
      }
    

    The React documentation shows this pattern in making calls to functions from expressions in attributes.

    0 讨论(0)
提交回复
热议问题