I am new to React JS. I wanted to convert the component state data into a JSON object. Is it possible? If so, how?
Sample state :
this.state = {
name : "Example",
age : "21",
description : "Some text here."
}
That's the whole question. I would appreciate it if you could tell me where I can find simple tutorials to do more advanced things in React (like sending/receiving JSON to/from a server, doing redirects, creating menus, etc). I know I sound vague, and I don't expect very specific answers(if any) to this.
To update our state, we use this. setState() and pass in an object. This object will get merged with the current state. When the state has been updated, our component re-renders automatically.
Just use JSON. stringify method in order to turn an javascript object into JSON text and store that JSON text in a string. Also, you can use JSON. parse which turns a string of JSON text into a Javascript object.
Always use the setState() method to change the state object, since it will ensure that the component knows it's been updated and calls the render() method.
Just use JSON.stringify
method in order to turn an javascript
object into JSON
text and store that JSON
text in a string.
Also, you can use JSON.parse
which turns a string of JSON text into a Javascript
object.
let state = {
name : "Example",
age : "21",
description : "Some text here."
}
console.log(state);
console.log(JSON.stringify(state));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With