Convert objects to React elements and elements to serializable objects
npm install json-reactConvert objects to React elements and elements to serializable objects
``sh`
npm i json-react
Convert React element to object
`js
import jsonReact from 'json-react'
const el = (
const obj = jsonReact.toObject(el)
`
`js`
// returns
{
type: 'div',
props: null,
children: [
{
type: 'h1',
props: null,
children: [
'Hello'
]
}
]
}
Convert object to React element
`js
import jsonReact from 'json-react'
const el = jsonReact.toElement({
type: 'div',
props: null,
children: [
{
type: 'h1',
props: null,
children: [
'Hello'
]
}
]
})
`
`js`
// returns
Hello
Convert object to React element with references to components
`js
import jsonReact from 'json-react'
import MyComponent from './MyComponent'
const el = jsonReact.toElement({
type: 'MyComponent',
props: {},
children: [
'Hello'
]
}, { MyComponent })
//
``
- Demonstrate how React elements are objects and can be converted to and from JSON
- Components can be serialized by displayName
- Components can be passed as scope to create elements from objects