ReactJs 从导航栏传递状态给子组件

huangapple go评论60阅读模式
英文:

ReactJs Pass State from Navbar to child components

问题

以下是翻译好的内容:

"Greetings and thank you for stopping by to check this. I'm having some issues and I don't really know how to fix this.

I have a react js app actually running with nextjs. So the thing is, I have a navbar, as a component, and a sign-up page as a component.

What I want is simple, when I click on the sign-up button on the navbar, the sign-up modal should pop up, and when I click on the close (x) icon on the modal, the modal should close.

So far, I've made it possible with useState to show the sign-up modal when the sign-up button is clicked.

Since the sign-up modal is a component that I'm importing into the navbar, I'm finding it difficult to use useState to close it when opened.

This is my code.

import SignUpModal from '../SignIn/Register';
function Navbar() {
  // Show Register Modal when the button is clicked
  const [register, setRegister] = useState(false);

  return (
    <>
      {register ? <SignUpModal /> : ''}
      <nav>
        <div>
          <Link>Home</Link>
          <Link>About</Link>
          <Link>Login</Link>
          <button
            type="button"
            onClick={() => setRegister(!register)}> Sign Up
          </button>
        </div>
      </nav>
    </>
  )
}

Now, when I click on the sign-up button, the modal that I imported is shown. But on the modal component, I can't close the modal because I can't pass the state from the navbar to the SignUp component and close it when I click on the close (x) icon.

In the SignUp component, I know I can do <button type="button" onClick={() => setNavbar(!navbar)}>Close</button>

But how do I pass it? Please, someone should help me, thanks!"

英文:

Greetings and thank you for stopping by to check this. I'm having some issues and I don't reall know how to fix this.

I have a react js app acctually running with nextjs. So the thing is, I have a navbar, as a compoents and a sign up page as a component.

What I want is simple, when I click on the sign up button on the navbar, the signUp modal should pop up, and when I click on the close (x) icon on the modal, the modal should close.

So far, I've made it possible with useState to show the signUp modal when the signUp button is clicked.

Since the signUp modal is a component that I'm importing to the navabr, I'm finding it difficult to useState to close it when opened.

This is my code.

import SignUpModal from &#39;../SignIn/Register&#39;;
function Navbar(
  // Show Reigster Modal when the button is clicked
  const [register, setRegister] = useState(false);

  return () {
   &lt;&gt;
     {register ? &lt;SignUpModal /&gt; : &#39;&#39;}
     &lt;nav&gt;
     &lt;div&gt;
      &lt;Link&gt;Home&lt;/Link&gt;
      &lt;Link&gt;About&lt;/Link&gt;
      &lt;Link&gt;Login&lt;/Link&gt;
      &lt;button
        type=&quot;button&quot;
        onClick={() =&gt; setRegister(!register)} &gt; Sign Up
      &lt;/button&gt;
    &lt;/div&gt;
   &lt;/nav&gt;
 &lt;/&gt;
 )
}

Now, when I click on the signUp button, the modal that I imported, is shown. But on the modal components, I can't close the modal because I can't pass the state from the navbar to the SignUp compoents and close it when I click on the close(x) icon.

On the SignUp component, I know I can do &lt;button type=&quot;button&quot; onClick={() =&gt; setNavbar(!navbar)}&gt;Close&lt;/button&gt;

But how do I pass it? Please someone should help me, thanks!

`

答案1

得分: 1

关闭模态框是在模态框组件内部完成的,您只需要将一个函数作为组件的prop传递给它,然后可以在关闭按钮的onClick()函数中调用该函数。

import SignUpModal from '../SignIn/Register';
function Navbar() {
  // 当按钮被点击时显示注册模态框
  const [register, setRegister] = useState(false);

  return (
    <>
      {register && <SignUpModal onClose={() => setRegister(false)} />}
      <nav>
        <div>
          <Link>Home</Link>
          <Link>About</Link>
          <Link>Login</Link>
          <button
            type="button"
            onClick={() => setRegister(true)}
          >
            Sign Up
          </button>
        </div>
      </nav>
    </>
  );
}

请注意,我已经按照您的要求,只翻译了代码部分。

英文:

For closing the modal from within the modal component, you just need to pass in a function as a prop to the component, which you can call within the onClick() function of the close button.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import SignUpModal from &#39;../SignIn/Register&#39;;
function Navbar(
  // Show Reigster Modal when the button is clicked
  const [register, setRegister] = useState(false);

  return () {
    &lt;&gt;
     {register &amp;&amp; &lt;SignUpModal onClose={() =&gt; setRegister(false)} /&gt;}
     &lt;nav&gt;
       &lt;div&gt;
        &lt;Link&gt;Home&lt;/Link&gt;
        &lt;Link&gt;About&lt;/Link&gt;
        &lt;Link&gt;Login&lt;/Link&gt;
        &lt;button
          type=&quot;button&quot;
          onClick={() =&gt; setRegister(true)} &gt; Sign Up
        &lt;/button&gt;
      &lt;/div&gt;
     &lt;/nav&gt;
    &lt;/&gt;
  }
 )
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 07:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定