目前正在做反应。我有两个组件,比如说 ad 和 home 。在家庭组件内部,我有一个图像,并且在该图像的单击事件中,我想在图像下方的家庭组件内呈现广告。有没有简单的方法。谢谢你!
3 回答
0
I think that will help to you.
export default class HomeComponent extends Component<Props> {
constructor(props) {
super(props);
this.state = {
renderAdComponent: false
};
this.onClickHandler = this.onClickHandler.bind(this);
}
onClickHandler() {
this.setState({renderAdComponent: !this.state.renderAdComponent})
}
render() {
return (
<View>
<Image onClick={this.onClickHandler}/>
{this.state.renderAdComponent ? <AdComponent/> : null}
</View>
);
}
}
于 2019-01-03T13:10:41.243 回答
0
@sdkcy 建议的内容还可以,但实际上并不需要三元运算符。您可以执行以下操作
{ this.state.isAdShown && <ComponentToShow /> }
这摆脱了无用的: null结果。
于 2019-01-03T14:10:11.453 回答
0
检查这个。我想这就是你想要的
//dynamically generate div
let DynamicDiv=()=>{
return (<div>
<p>i am here</p>
</div>)
}
class App extends Component {
constructor(props){
super(props)
this.state={
visible:false //visibility of Component
}
this.divVisiblity=this.divVisiblity.bind(this) //function is bind when user clicks on pic
}
divVisiblity(){
//this function will get called when user clicks on function
this.setState(()=>{return {visible:!this.state.visible}}) //changes visible state of Component when user clicks
}
render() {
return (
<div>
<div className="App">
{/* onClick is assigned function named divVisiblity */}
<img onClick={this.divVisiblity} src="https://placekitten.com/g/200/300" alt="test"/>
{/*this is ternary if else statement in js */}
{/* if visible = true ,display Component else dont */}
<div>
{this.state.visible && <DynamicDiv/>}
</div>
);
}
}
于 2019-01-03T19:01:58.717 回答