在onClick
事件之后,我试图在react组件中呈现两个react元素时遇到了问题。想知道这是否可能吗?我确信我搞错了三元运算符,但我不能想出另一种方法来做我想要做的事情?
TL;DR:“当我单击一个按钮时,我会看到elementA 和 elementB”
以下是代码的一小段:
import React, { Component } from 'react';
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = { showElement: true };
this.onHandleClick = this.onHandleClick.bind(this);
}
onHandleClick() {
console.log(`current state: ${this.state.showElement} and prevState: ${this.prevState}`);
this.setState(prevState => ({ showElement: !this.state.showElement }) );
};
elementA() {
<div>
<h1>
some data
</h1>
</div>
}
elementB() {
<div>
<h1>
some data
</h1>
</div>
}
render() {
return (
<section>
<button onClick={ this.onHandleClick } showElement={this.state.showElement === true}>
</button>
{ this.state.showElement
?
null
:
this.elementA() && this.elementB()
}
</section>
)
}
}
export default MyComponent;
https://stackoverflow.com/questions/44906363
复制相似问题