Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
react-json-renderer
Advanced tools
Basic converter of a React component tree to a JS object or JSON string and associated renderer using provided components.
yarn add react-json-renderer # Yarn
npm install react-json-renderer # npm
React is a required peer dependency, make sure to install it as well if you haven't already:
yarn add react # Yarn
npm install react # npm
convertToObject(
tree: React.Element<*>,
params?: {
processProps?: (props: Object) => Object,
replacers?: {
[string]: (props: Object) => React.Element<*>,
},
},
): Object
Converts a component tree to a formatted Object supported by the renderFromObject()
function.
convertToJSON(
tree: React.Element<*>,
params?: {
processProps?: (props: Object) => Object,
replacers?: {
[string]: (props: Object) => React.Element<*>,
},
space?: number | string,
},
): Object
Converts a component tree to a JSON string supported by the Renderer
component.
renderFromObject(
tree: Object,
params?: {
components?: {
[type: string]: Class<React.Component<*, *, *>> | (props: Object) => React.Element<*>,
},
fallback?: Class<React.Component<*, *, *>> | (props: Object) => React.Element<*>,
},
): React.Element<*>
Converts a formatted Object to a React elements tree ready to be rendered by React.
Props:
json?: string
: Formatted Object tree in JSON.tree?: Object
: Formatted Object tree.Server or other source
import React from 'react'
import { convertToJSON } from 'react-json-renderer'
// React components or simple strings can be converted
const Text = 'Text'
const View = ({ children }) => children
const Welcome = ({ name }) =>
<View>
<Text>Welcome {name}!</Text>
</View>
// Returns the JSON payload to provide to the client
export const createWelcome = (name) => convertToJSON(<Welcome name={name} />)
Web client
import React from 'react'
import { Renderer } from 'react-json-renderer'
// Components from the payload supported by the client
const components = {
Text: ({ children }) => <span className='text'>{children}</span>,
View: ({ children }) => <div className='view'>{children}</div>,
}
// Fallback for unsupported components, if not provided it will return null and therefore not render the component and its children
const Fallback = ({ children }) => <div className='fallback'>{children}</div>
// Inject the JSON payload from the server and render with provided component and fallback
export const PayloadRenderer = ({ payload }) =>
<Renderer components={components} fallback={Fallback} json={payload} />
React-Native client
import React from 'react'
import { Renderer } from 'react-json-renderer'
import { StyleSheet, Text, View } from 'react-native'
const styles = StyleSheet.create({
text: {
fontSize: 14,
},
view: {
flex: 1,
},
})
const RenderText = ({ children }) => <Text style={styles.text}>{children}</Text>
const RenderView = ({ children }) => <View style={styles.view}>{children}</View>
const components = {
Text: RenderText,
View: RenderView,
}
export const PayloadRenderer = ({ payload }) =>
<Renderer components={components} fallback={RenderView} json={payload} />
MIT
See LICENSE file.
FAQs
Render React components to a JSON tree
The npm package react-json-renderer receives a total of 33 weekly downloads. As such, react-json-renderer popularity was classified as not popular.
We found that react-json-renderer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.