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

215
Views
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 answers
Answer question

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 Report

0

You can do it with string concatenation:

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

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 Report
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!