Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cawfree/react-jsx-provider

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cawfree/react-jsx-provider

A React Provider for reliably rendering dependency-aware distributed JSX.

  • 1.0.25
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@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',
  },
});

// XXX: This file contains a package.json-esque JSON which
//      defines the dependencies of the JSX wished to embed
//      within the application..
const request = require('./assets/json/package.json');

export default class App extends React.Component {
  constructor(nextProps) {
    super(nextProps);
    this.__renderFailure = this.__renderFailure.bind(this);
  }
  // XXX: This is the fallback render method for when a <ScriptComponent/>
  //      fails to have its dependency requirements met.
  __renderFailure(resolutionErrors) {
    return (
      <View
        style={styles.error}
      >
        <Text
          style={styles.errorText}
        >
          {'?'}
        </Text>
      </View>
    );
  }
  // XXX: Define the runtime implementations for each library dependency
  //      that you wish to expose to a <ScriptComponent/>.
  __getRuntime() {
    return {
      ...require('./package.json'),
      "config": {
        "react-native": {
          // XXX: Try commenting out some of the dependencies!
          View,
          Text,
          Image,
        },
      },
    };
  }
  render() {
    // XXX: The Provider is used to define the runtime implementation context
    //      for all of the child <ScriptComponent/>s. Use this at the root of
    //      your application, or nest multiple instances to define child-specific
    //      runtime dependencies.
    //
    //      The "script" prop is used to select which JSX string to render within
    //      the ScriptComponent. This is defined as part of the request package.json.
    //
    //      Try changing it to the name of a script that doesn't exist!
    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

Keywords

FAQs

Package last updated on 27 Sep 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc