import React, { Component } from ‘react’;
class IF extends React.Component {
constructor(props) {
super(props);
this.state = {
show: false
}
}
// 渲染函数,this 指向实例本身
render() {
let display = this.display.bind(this)//改变this指向
return <div>
{/* 这种方法省略了 this 绑定的过程 */}
<button onClick={display}>{this.state.show ? ‘点击隐藏’ : ‘点击显示’}</button>
{this.state.show ? <p>显示出来啦</p> : null}
</div>
}
display() {
this.setState({
show: !this.state.show
})
}
}
export default IF;
原文始发于:
主题测试文章,只做测试使用。发布者:千寻,转转请注明出处:http://www.cxybcw.com/141490.html