React无法读取未定义的属性"bind" [英] React Cannot read property 'bind' of undefined

查看:229
本文介绍了React无法读取未定义的属性"bind"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了绑定无法理解的问题.在StackOverflow中,我尝试了所有与该问题相关的所有问题的绑定方式,但是每次我都使用相同的方法

I am stuck with an issue that I don't understand with binding. I tried all ways of binding in all questions relative to this issue in StackOverflow but every time I have the same

错误:反应无法读取未定义的属性'bind'"

错误2:" TypeError:无法读取Object.getClosestInstanceFromNode"

Error2:"TypeError: Cannot read property '__reactInternalInstance$b7iw1elmz95' of null at Object.getClosestInstanceFromNode"

因为我已经尝试了所有方法,所以我想知道绑定外部问题是否是一个真正的问题.

Because I've tried everything, I wonder if is that a real problem with the binding of an external problem.

我想做的是,当我单击一个按钮时,出现另一个内容.

What I want to do is when I click on a button, another content appear.

这是我的代码:

import React, {Component} from 'react';

export default class Projects extends Component {
  constructor(){
    super();

    this.state = {
      onShow: false,
      opacity: 0,
      height: 0
   }
 }

  OnShow(){
    this.setState({
      onShow: !this.state.onShow,
      opacity: this.state.opacity === 0 ? 1:0,
      height: '100vh'
    });
  }

  render(){

    return(
      <div>
        <h2>blabla</h2><p>some extra blabla</p>
        <button onClick={this.onShow.bind(this)}>
          <div opacity={this.state.opacity}>YO</div>
        </button>
      </div>
    );
  }
}

推荐答案

onClick按钮上有一个错字: this.OnShow.bind(this)是正确的方法.

There is a typo in onClick on button: this.OnShow.bind(this) is the right way.

函数命名为OnShow,状态var命名为onShow

Function is named OnShow, the state var is named onShow

这篇关于React无法读取未定义的属性"bind"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆