React按钮点击应更改DIV的显示值
我得到了一个带有按钮的Navbar,请更改登录表单的显示值。登录表单和登录表单是一个不同的文件,Navbar是一个不同的文件,并且应该显示的主页是一个不同的文件。这些是每个变体的最小变体,因此您有一些详细了解我的问题:
主页:
const HomePage = () => {
return (
<div>
<Navbar />
<Login />
<div id="content">
</div>
</div>
);
}
navbar:
const Navbar= () => {
const showLogin = () => {
document.getElementById('Login').style.display='block';
}
return (
<div id="Navbar">
<NavLink activeClassName="active" to='/'><img src={logo}/></NavLink>
<ul>
...
</ul>
<ul>
<button onClick={showLogin}>Anmelden</button>
</ul>
</div>
);
}
登录形式:
const Login = () => {
return (
<div id="Login">
<form>
<label>Anmelden</label>
<label for="username">Nutzername</label>
<input name="username" type="text"></input>
<label for="pw">Passwort</label>
<input name="pw" type="password"></input>
<button type="submit">Login</button>
</form>
</div>
);
}
有没有办法实现此目的,或者我最简单的选择是将登录源代码包括在Navbar源代码?
I got a Navbar which has a button do change the display value of a login form. The Login form and the Login form is a diffrent file, the navbar is a diffrent file and the homepage where it should be display is a diffrent file. Those are the minimal variants of each so that you got some got to understand my problem in detail:
Homepage:
const HomePage = () => {
return (
<div>
<Navbar />
<Login />
<div id="content">
</div>
</div>
);
}
Navbar:
const Navbar= () => {
const showLogin = () => {
document.getElementById('Login').style.display='block';
}
return (
<div id="Navbar">
<NavLink activeClassName="active" to='/'><img src={logo}/></NavLink>
<ul>
...
</ul>
<ul>
<button onClick={showLogin}>Anmelden</button>
</ul>
</div>
);
}
Login-Form:
const Login = () => {
return (
<div id="Login">
<form>
<label>Anmelden</label>
<label for="username">Nutzername</label>
<input name="username" type="text"></input>
<label for="pw">Passwort</label>
<input name="pw" type="password"></input>
<button type="submit">Login</button>
</form>
</div>
);
}
Is there a way to achieve this, or would my easiest option be to include the Login source code into the Navbar source code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无需将登录组件移动到Navbar中。保持原样。
您可以使用
usestate
和props
切换CSS类以显示/隐藏您的登录组件。我在此。步骤:
隐藏
和block
hidden
block
如果是真的。onClick
创建道具。usestate
,该具有布尔值。布尔值将其传递到登录页面prop,然后使用onclick
prop从navbar中切换到布尔状态You do not need to move your Login component inside Navbar. Keep it as it is.
You can use
useState
andProps
to switch css classes to show/hide your Login component. I have created very simple solution for you in this CodeSandbox.Steps:
hidden
andblock
hidden
toblock
if true.onClick
in the Login component.useState
inside your Homepage which holds a boolean value. That boolean value pass it to the Login page prop and then useonClick
prop from Navbar to switch that boolean state是的,根据您的CSS系统,仅通过使用它就很容易实现。
反应解决方案是使用 refs 。
简单的方法是在父组件中创建
ref
,然后将其作为道具将其传递给两个组件:homepage(ie parent),创建一个像so so so so so
loginref = useref();
然后将其作为道具向2个孩子传递。in
login-form.js
您在div上分配了iD登录的prop,例如so&lt; div id ='login'ref = {props.loginref}&gt;
navbar.js
中您可以使用该prop更改其显示值,例如constshowlogin =()=&gt; {props.loginref.current.style.display ='block';}
nb:这是一种快速简便的方法,不是最好的练习,但可以完成工作。这里的最佳实践是使用 forffersRefrer“ href =“ https://reactjs.org/docs/hooks-reference.html#useimperativehandle” rel =“ nofollow noreferrer”> useimperative horeflovely 。准备好时,请仔细阅读文档。
Yes, depending on your CSS system this is easily achievable just by using that.
The React solution is using refs.
The easy way is to create a
ref
in the parent component and then pass it down as a prop to both components:In Homepage (i.e. parent), create a ref like so
loginRef = useRef();
then pass it down as a prop to the 2 children.In
Login-Form.js
you assign that prop on the div with id Login like so<div id='Login' ref={props.loginRef}>
Then in
Navbar.js
you can use that prop to change its display value like so constshowLogin = () => {props.loginRef.current.style.display='block';}
NB: This is a fast and easy way, not best practice but it gets the work done. The best-practice here is to use forwardRef and - super advanced - useImperativeHandle. Take your time and go through the documentation when you're ready.
,因为主动设置为false。
登录页面将首先显示“
不活动”
它
Login page will show "it is not active" first because active is set to false.but once you click on submit button it will show "it is active"
HomePage
Login
Navbar