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

262
Vistas
React-select initial value not being set when retrieved from postgres table

I am using react-select and I am trying to set my value prop to pre-existing value for that record.

I am retrieving the data from a postgres db and it is be returned as follows:

"{value:2,label:'Dog'},{value:4,label:'Cat'}"

What I am then attempting to do is push this to an array as follows:

let data = "{value:2,label:'Dog'},{value:4,label:'Cat'}"
let selectValues = []
selectValues.push(data)
console.log(selectValues)

The console log for selectValues array returns the following result when expanding the array in dev tool's console:

0: "{value:2,label:'Dog'},{value:4,label:'Cat'}"

When I then attempt to assign this array to my react-select component value below, the initial values are not being displayed.

The thing is though, if I hardcode the array object as shown against the commented out line, for value, it works.

Not sure what I am missing with my approach above with pushing the string object to my selectValues array.

<Select 
  name="my-select"
  styles={customStyles}
  options={options}
  isMulti={true}
  value={selectValues}
  // value={[{value:2,label:'Dog'},{value:4,label:'Cat'}]}
  placeholder={'Options....'}
/>   

  
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. I have the feelings that you are receiving a JSON from the DB, so you have to parse it.

For more information read here

about 4 years ago · Juan Pablo Isaza Denunciar
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