React按钮点击应更改DIV的显示值

发布于 2025-01-31 12:33:21 字数 1521 浏览 3 评论 0原文

我得到了一个带有按钮的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2025-02-07 12:33:21

您无需将登录组件移动到Navbar中。保持原样。
您可以使用usestateprops切换CSS类以显示/隐藏您的登录组件。我在此

步骤:

  1. 创建两个CSS类隐藏block
  2. 在您的登录组件中添加一个布尔值添加一个布尔prop,该prop prop class hidden block如果是真的。
  3. 在登录组件中为onClick创建道具。
  4. 在主页内创建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 and Props to switch css classes to show/hide your Login component. I have created very simple solution for you in this CodeSandbox.

Steps:

  1. Create two CSS classes hidden and block
  2. In your Login component add a boolean prop which switches class hidden to block if true.
  3. Create a prop for onClick in the Login component.
  4. Create a useState inside your Homepage which holds a boolean value. That boolean value pass it to the Login page prop and then use onClick prop from Navbar to switch that boolean state
怪我太投入 2025-02-07 12:33:21

是的,根据您的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;


  • < < p>然后在navbar.js中您可以使用该prop更改其显示值,例如const showlogin =()=&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 const showLogin = () => {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.

装纯掩盖桑 2025-02-07 12:33:21

,因为主动设置为false。

登录页面将首先显示“

const HomePage = () => {
    const[active,setActive]=useState(false);
    return (
        <div>
            <Navbar activesetter={setActive} />
            <Login activestatus={active} />
            <div id="content">
            </div>
        </div>
    );
}

不活动”

const Login = (props) => {

    return(
    <div>
        <div>
        {props.activestatus ? "it is active" : "it is not active" }
        </div>
    </div>

    );

}

const Navbar = (props) => {
    const handleSubmit=(e)=> {
            e.preventDefault();
            props.activesetter(true);
        
    }
    return(
    <div>
        <form  onSubmit={handleSubmit}>
        <button  type="submit">Login</button>

        </form>
    </div>

    );

}

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

const HomePage = () => {
    const[active,setActive]=useState(false);
    return (
        <div>
            <Navbar activesetter={setActive} />
            <Login activestatus={active} />
            <div id="content">
            </div>
        </div>
    );
}

Login

const Login = (props) => {

    return(
    <div>
        <div>
        {props.activestatus ? "it is active" : "it is not active" }
        </div>
    </div>

    );

}

Navbar

const Navbar = (props) => {
    const handleSubmit=(e)=> {
            e.preventDefault();
            props.activesetter(true);
        
    }
    return(
    <div>
        <form  onSubmit={handleSubmit}>
        <button  type="submit">Login</button>

        </form>
    </div>

    );

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文