Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

You can do it with string concatenation:

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda