javascript - 在 React 中隐藏/显示容器以进行登录
问题描述
我在我的代码中添加了一个登录/注册容器。
Login.class
const Form = ({ initialState = STATE_LOG_IN }) => {
const [mode, toggleMode] = useToggle(initialState);
return (
<Container pose={mode === STATE_LOG_IN ? "signup" : "login"}>
<div className="container__form container__form--one">
<FormLogin mode={mode} />
</div>
<div className="container__form container__form--two">
<FormSignup mode={mode} />
</div>
<Overlay toggleMode={toggleMode} mode={mode} />
</Container>
);
};
export default Form;
当用户单击登录/注册时,此容器是我标题中的调用者,但我不知道如何从登录单击触发显示?
标头代码:
class Header extends React.Component {
constructor(props, context) {
super(props);
this.state = {
showLogin: false};
}
openLogin = () => {
this.setState({showLogin: !this.state.showLogin});
}
const menuLoginRegister = <Nav.Link
ref="LoginRegisterModal" eventKey={1}
href="#"
onClick={this.openLogin}
bsPrefix="header-menu-item"
>{TextContents.MenuLoginRegister}</Nav.Link>;
return (
...
<NavBar>
....
{menuLoginRegister}
</NavBar>
<LoginRegisterForm show={this.state.showLogin}
onHide={() => this.setState({ showLogin: false })}/>
</div>
这段代码中的显示被const Form
从login
类中捕获。
知道如何从标题中控制登录类的可见性吗?
谢谢
解决方案
你可以使用 CSS 来显示/隐藏你的组件,方法是根据你的状态添加一个特定的类。像这样的东西:
<LoginRegisterForm className={this.state.showLogin ? 'hidden' : ''}/>
如果是,这将为hidden
您的组件添加类。在您的 CSS 中,只需将此类定义为:this.state.showLogin
true
.hidden {
display: none;
}
推荐阅读
- c - libcurl curl_multi CURLOPT_POSTFIELDS 内存管理
- java - 仅针对某些 DIV 缩放 Android WebView
- c++ - 在 C++ 中使用 goto 语句破坏变量是如何发生的?
- c# - .net 缺少方法异常 .CTOR
- linux - 在 cmus 中播放音乐文件夹(不添加到库中)
- python - 如何防止在Python中从firestore数据库中获取数据时出现的值错误
- c++ - C++ 程序不允许我提供第二个输入
- python - 错误:mysql:无法下载资源“tcl-tk--tcllib”
- python - 如何使用 HTML 的输入
- spring-boot - Mapstruct:考虑在你的配置中定义一个“FooMapper”类型的bean