简体   繁体   中英

Converting component state to JSON in React

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM