简体   繁体   English

React Button Click应该改变div的显示值

[英]React Button Click should change the display value of div

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?有没有办法实现这一点,或者我最简单的选择是将登录源代码包含到导航栏源代码中?

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.您可以使用useStateProps来切换 css 类以显示/隐藏您的登录组件。 I have created very simple solution for you in this CodeSandbox .我在这个CodeSandbox中为您创建了非常简单的解决方案。

Steps:脚步:

  1. Create two CSS classes hidden and block创建两个 CSS 类hiddenblock
  2. In your Login component add a boolean prop which switches class hidden to block if true.在您的登录组件中添加一个布尔属性,如果为真,则将hidden类切换为block
  3. Create a prop for onClick in the Login component.在 Login 组件中为onClick创建一个 prop。
  4. Create a useState inside your Homepage which holds a boolean value.在您的主页内创建一个包含布尔值的useState That boolean value pass it to the Login page prop and then use onClick prop from Navbar to switch that boolean state该布尔值将其传递给登录页面道具,然后使用导航栏中的onClick道具来切换该布尔状态

Yes, depending on your CSS system this is easily achievable just by using that.是的,根据您的 CSS 系统,这很容易通过使用它来实现。

The React solution is using refs . React 解决方案是使用refs The easy way is to create a ref in the parent component and then pass it down as a prop to both components:简单的方法是在父组件中创建一个ref ,然后将其作为 prop 传递给两个组件:

  • In Homepage (ie parent), create a ref like so loginRef = useRef();在主页(即父级)中,像这样创建一个引用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}>Login-Form.js中,您在 div 上使用 id Login 分配该道具,如下所示<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';}然后在Navbar.js中,您可以使用该道具来更改其显示值,例如 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 .这里的最佳实践是使用forwardRef和 - 超级高级 - useImperativeHandle Take your time and go through the documentation when you're ready.准备好后,花点时间阅读文档。

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"登录页面将首先显示“它不活动”,因为活动设置为 false。但是一旦您单击提交按钮,它将显示“它是活动的”

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>

    );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM