Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda