Search code examples
javascriptreactjsreact-hookspopup

How to control hooks between 2 components


I wanted to know if I can send from one component to another property to define a state. Like a popup, I have a "Start now" button and I want to popup the reg form and make the "start now" button disappear and to close the popup in when I click on an logo img in the form. ex:

first component

function RegisterPage (props){
    return(
      <form className="All-Register">
        <div className="Register-Header">
          <img src={Logo} name="Logo" onClick={() => props.setIsOpen(!props.isOpen)}/>
        </div>
        <div className="Register-Form">
          <input type="text" placeholder="nick_name" />
          <input type="text" placeholder="email" />
          <input type="password" placeholder="pass" />
          <input type="submit" value="register" />
        </div>
        <div className="Forgot">
          <span>forgot pass</span>
        </div>
      </form>
    )
}
export default RegisterPage;

second component

function RegisterButton(){
  const [isOpen, setIsOpen] = useState(false);
  return(
    <div>
      {isOpen ? <RegisterPage isOpen = {isOpen} /> :
      <div className="HomeWrapper">
        <div className="para">
          <p>Build the strongest army</p>
          <p>and join the fight-club</p>
        </div>
        <div className="div-btn">
          <button className="Reg-Btn" onClick={() => setIsOpen(!isOpen)}>start now</button>
        </div>
      </div>
    }
    </div>
  );
}

export default RegisterButton;

ty for your help :)


Solution

  • Add a new onLogoClick prop to RegisterPage which is attached the the onClick of the <img/>. Then bind this in the RegisterButton parent to a function which set the state to false.

    function RegisterPage (props){
        return(
          <form className="All-Register">
            <div className="Register-Header">
              <img src={Logo} name="Logo" onClick={props.onLogoClick}/>
            </div>
            <div className="Register-Form">
              <input type="text" placeholder="nick_name" />
              <input type="text" placeholder="email" />
              <input type="password" placeholder="pass" />
              <input type="submit" value="register" />
            </div>
            <div className="Forgot">
              <span>forgot pass</span>
            </div>
          </form>
        )
    }
    export default RegisterPage;
    
    function RegisterButton(){
      const [isOpen, setIsOpen] = useState(false);
      return(
        <div>
          {isOpen ? <RegisterPage isOpen={isOpen} onLogoClick={() => setIsOpen(false)}/> :
          <div className="HomeWrapper">
            <div className="para">
              <p>Build the strongest army</p>
              <p>and join the fight-club</p>
            </div>
            <div className="div-btn">
              <button className="Reg-Btn" onClick={() => setIsOpen(!isOpen)}>start now</button>
            </div>
          </div>
        }
        </div>
      );
    }
    
    export default RegisterButton;