我在下面有这个简单的代码。当我按下切换按钮时,组件 Child 应该隐藏/显示,但事实并非如此。
我必须重新渲染一些东西吗?我不想切入/切出 CSS 类,只需单击按钮即可切换
import React, {Component} from 'react';
let active = true
const handleClick = () => {
active = !active
}
class Parent extends React.Component {
render() {
return (
<div>
<OtherComponent />
{active && <Child />}
<button type="button" onClick={handleClick}>
Toggle
</button>
</div>
)
}
}
class Child extends React.Component {
render() {
return (
<div>
I am the child
</div>
)
}
}
class OtherComponent extends React.Component {
render() {
return (
<div>
I am the OtherComponent
</div>
)
}
}