进入下一页后如何在 React 中存储布尔值?
我正在使用 React Router 设置我的联系页面(由多个页面、嵌套路由器组成):
<Route path="/contact" element={<Contact />}>
<Route path="name" element={<ContactName />} />
<Route path="email" element={<ContactEmail />} />
<Route path="message" element={<ContactMessage />} />
</Route>
当我第一次进入我的联系页面时,我有一个按钮可以转到 ContactName 页面(这样他们就可以输入他们的名字) :
<a
href="/contact/name"
className="contact-icon-color contact-icon-link"
onClick={() => { setContactButton(false) }}>
</a>
当他们单击按钮时,我将按钮链接的显示属性设置为无。这就像我想要的那样。但是,当它移至 ContactName 页面时,该按钮会再次出现。我知道布尔值 contactButton 在我的功能组件中使用以下行重置:
const [contactButton, setContactButton] = useState(true);
我不知道如何克服这个问题。我的问题是:
- 是否有更好的替代方法来删除按钮或移至下一页?
- 如果没有,我将如何存储 contactButton 的最新值,以便链接不会重新出现?
谢谢,请注意我是 React 新手。
I am using React Router to set up my contact page (which consists of multiple pages, nested router):
<Route path="/contact" element={<Contact />}>
<Route path="name" element={<ContactName />} />
<Route path="email" element={<ContactEmail />} />
<Route path="message" element={<ContactMessage />} />
</Route>
When I first go into my contact page, I have a button to move on to the ContactName page (so they can enter their name):
<a
href="/contact/name"
className="contact-icon-color contact-icon-link"
onClick={() => { setContactButton(false) }}>
</a>
When they click the button, I set the display property of the button link to none. This works like I intended. However, when it moves onto the ContactName page, the button reappears again. I understand that the boolean value contactButton is reset with the followoing line in my functional component:
const [contactButton, setContactButton] = useState(true);
I do not know how to overcome this. My questions are:
- Is there a better alternative way to remove the button or move onto the next page?
- If not, how would I store the latest value for contactButton so that the link does not reappear?
Thanks, please note that I am new to React.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,你可以在这里使用
props
。首先,将
PropTypes
导入到您的项目中。https://www.npmjs.com/package/prop-types
您的代码类似如下所示:
Ps:请检查导入或小错误,我试图解释您的问题。我想,这是更好/先进的方式,而不是为本地存储设置值
As I know, you can play with
props
here.First, import
PropTypes
to your project.https://www.npmjs.com/package/prop-types
Your code similarly looks like the following:
P.s: Pls, check imports or minor errors, I tried to explain what you asked. And it is, I guess, much better/advanced way rather setting value to localstorage