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

219
Vistas
Working with Strings and useState variables in react js?

I have two useStates, which stores the values taken from the user.

const [classname, setClassname] = useState('');
const [name, setName] = useState('');

Similarly I have a string that has to be passed as body for the Rest API post request.

const string = '{ \ "class": "Device", \ "name": "some name", \ "instanceNumber": 0, \ 
                  "properties": [ \ \ ] \ }';

I have to pass the classname in place of device and and name in place of some name in the string. How can I change the string according to the useState values.

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

0

You can use string interpolation for this, it is process of embedding an expression into part of a string:

const string = `{ \ "class":"${classname}", \ "name": "${name}", \ "instanceNumber": 0, \ 
                  "properties": [ \ \ ] \ }`;

Here is a reference to learn more https://dmitripavlutin.com/string-interpolation-in-javascript/

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it with string concatenation:

const string = `{ \"class": ${classname}, \"name": ${name}, \"instanceNumber": 0, \"properties": [ \ \] \ }`;
about 4 years ago · Juan Pablo Isaza Denunciar

0

const string = `{ \ "class": ${classname}, \ "name": ${name}, \ "instanceNumber": 0, \ 
                  "properties": [ \ \ ] \ }`;

But if you are sending it to an api as a post request I would recommend sending it as a object as it would be easier to access the values in the backend. As a object:

const string = { class: classname, name: name, instanceNumber: 0, properties: []};
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