@cawfree/react-jsx-provider
A React <Provider/>
used to reliably rendering dependency-aware JSX. Compatible with both react
and react-native
.
🤔 About
This library is built on top of the awesome react-jsx-parser
, which is used to take a raw JSX string and render it as part of the React DOM, and adds a couple of utilities to enhance the scalability and portability of the JSX. This is done by defining a package.json
-esque string which defines not only the content to render, but the necessary data dependencies of the runtime environment.
If all of the dependencies are met by the runtime, the JSX string can be injected and rendered within the DOM; otherwise, it falls back to a renderFailure
method, which allows your app to continue as normal. Since it is backed by a React.createContext
<Provider/>
, these runtime dependencies can be referenced or overriden throughout the nested hierarchy.
🚀 Getting Started
Using npm
npm install --save @cawfree/react-jsx-provider
Using yarn
yarn add @cawfree/react-jsx-provider
✍️ Example
import React from 'react';
import {
View,
Text,
StyleSheet,
Image,
} from 'react-native';
import Provider, { ScriptComponent } from '@cawfree/react-jsx-provider';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
},
error: {
flex: 1,
backgroundColor: 'lightgrey',
alignItems: 'center',
justifyContent: 'center',
},
errorText: {
fontSize: 60,
fontWeight: 'bold',
color: 'white',
},
});
const request = require('./assets/json/package.json');
export default class App extends React.Component {
constructor(nextProps) {
super(nextProps);
this.__renderFailure = this.__renderFailure.bind(this);
}
__renderFailure(resolutionErrors) {
return (
<View
style={styles.error}
>
<Text
style={styles.errorText}
>
{'?'}
</Text>
</View>
);
}
__getRuntime() {
return {
...require('./package.json'),
"config": {
"react-native": {
View,
Text,
Image,
},
},
};
}
render() {
return (
<Provider
renderFailure={this.__renderFailure}
request={request}
runtime={this.__getRuntime()}
>
<View
style={styles.container}
>
<ScriptComponent
script="Welcome"
/>
</View>
</Provider>
);
}
}
Check out the React Native app in the examples folder for more info.
🙏 Acknowledgements
react-jsx-parser
semver
✌️ License
MIT