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

129
Views
How to add select option to localstorage ReactJS?

I get the selected select value through onchange, store it in state and save it in react and use it further where I need this data and everything works fine. After reloading the select value becomes the default. I don't understand how can I store the select in localstorage.

class Currency extends Component {
  constructor(props){
    super(props);
    this.state = {
      currencies: ["$ USD", "€ EURO"],
    }
  }
    
    componentDidMount = async () =>{
      const response = await client.query({
        query: Currency_Query
      })
      this.setState({
        currencies:response.data.currencies
      })
    }
    
    render() {
      return(
        <div>
          <div className={s.select}>
             <select > 
            {this.state.currencies.map((currencies) =>(
                <option>
                    <div className={s.option}>
                    {currencies.symbol} {currencies.label}
                    </div>                  
                </option>
                )
              ) 
            }
                </select> 
                
        </div>
        </div>
            )}
    }
}

export default Currency;

In app.js, through onChange, I get the values, save them in state, and then save them in localStorage

handleChange = (e) => {
    this.setState({ selectValue: e.target.value })
  }
componentDidMount(){
    this.Data = JSON.parse(localStorage.getItem('data'));
    if(localStorage.getItem('data')){
      this.setState({
        selectValue: this.Data.selectValue,

      })
    } else {
      this.setState({
      selectValue: "$ USD",
      })
    }
  }
  componentWillUpdate(nextProps, nextState){
    localStorage.setItem('data', JSON.stringify(nextState))
  }
about 4 years ago · Juan Pablo Isaza
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!