Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
How to store a boolean value in React after moving onto the next page?

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:

  1. Is there a better alternative way to remove the button or move onto the next page?
  2. 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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Route, Link } from 'react-router-dom';

export const Contact = ({ isVisibleButton }) => {
  const [contactButton, setContactButton] = useState(isVisibleButton);

  return(
    <Link to="/contact/name"
     className="contact-icon-color contact-icon-link" 
     onClick={() => { setContactButton(!isVisibleButton) }} />
    
    {contactButton && (
     <button>something you want</button>
    )}
  )
}

Contact.propTypes = {
  isVisibleButton: PropTypes.bool,
};

Contact.defaultProps = {
  isVisibleButton: true,
};

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!