英文:
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 '../SignIn/Register';
function Navbar(
// Show Reigster 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 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 <button type="button" onClick={() => setNavbar(!navbar)}>Close</button>
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 '../SignIn/Register';
function Navbar(
// Show Reigster Modal when the button is clicked
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>
</>
}
)
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论