4. 条件渲染

This commit is contained in:
shikong 2024-10-23 21:37:56 +08:00
parent c3c091aad5
commit f7d1029fe6
Signed by: Shikong
GPG Key ID: BD85FF18B373C341
2 changed files with 42 additions and 0 deletions

View File

@ -1,5 +1,6 @@
.App {
text-align: center;
overflow: auto;
}
.App-logo {

View File

@ -115,6 +115,45 @@ function App() {
e.stopPropagation()
}
class IfComponent extends React.PureComponent {
state = {
show: true
}
show = ()=>{
this.setState((state)=>({
...state,
show: true
}))
}
hide = ()=>{
this.setState((state)=>({
...state,
show: false
}))
}
render(){
return <div style={{border: '1px solid', width: '80%', overflow: 'auto'}}>
<div>条件渲染</div>
{this.state.show && <div> IfComponent 显示 </div>}
<div style={{display: 'flex', justifyContent: "center"}}>
<button onClick={this.hide}>隐藏</button>
<button onClick={this.show}>显示</button>
</div>
<button onClick={()=>{
this.setState((state)=>({
...state,
show: !state.show
}))
}}>切换</button>
</div>
}
}
return (
<div className="App">
<header className="App-header">
@ -149,6 +188,8 @@ function App() {
<button onClick={clickHandler3.bind(this, "按钮3")}>按钮3</button>
<button onClick={(e) => clickHandler3("按钮3", e)}>按钮3</button>
</div>
<IfComponent/>
</header>
</div>
);