Giter VIP home page Giter VIP logo

react-from-json's Introduction

react-from-json

Declare your React component tree in JSON

NPM JavaScript Style Guide

Example

Intro

react-from-json lets you render React

<Burger chain="Wahlburger">
  <Patty variant="impossible" />
</Burger>

from JSON

{
  "type": "Burger",
  "props": {
    "chain": "Wahlburger",
    "children": {
      "type": "Patty",
      "props": {
        "variant": "impossible"
      }
    }
  }
}

It also supports non-recursive structures.

Install

npm install --save react-from-json

Usage

import React from "react";
import ReactFromJSON from "react-from-json";

const entry = {
  type: "Burger",
  props: {
    chain: "Wahlburger",
    children: {
      type: "Patty",
      props: {
        variant: "Impossible"
      }
    }
  }
};

const mapping = {
  Burger: ({ chain, children }) => (
    <div>
      <h1>{chain}</h1>
      <div>{children}</div>
    </div>
  ),
  Patty: ({ variant }) => <span>{variant}</span>
};

const Example = () => {
  return <ReactFromJSON entry={entry} mapping={mapping} />;
};

Props passed to your components

Props passed to your mapped components include

  • propKey - name of the prop that rendered your component
  • propIndex - index of your component if using flat trees
  • _type - the type value for your component
  • ...props - the resolved value of your props object, with relevant child nodes rendered as components

Other JSON shapes

If your data doesn't follow the type | props shape, react-from-json makes it easy to map your data on the fly using the mapProp prop.

import React from "react";
import ReactFromJSON from "react-from-json";
import mapping from "./mapping";

const entryWithDifferentShape = {
  _type: "Burger",
  chain: "Wahlburger",
  children: {
    _type: "Patty",
    variant: "Impossible"
  }
};

const mapProp = prop => {
  if (prop._type) {
    const { _type, ...props } = prop;

    return {
      type: _type,
      props
    };
  }

  return prop;
};

const Example = () => {
  return (
    <ReactFromJSON
      entry={entryWithDifferentShape}
      mapping={mapping}
      mapProp={mapProp}
    />
  );
};

Flat trees

react-from-json also supports flat, non-recursive structures via the special <ComponentLookup /> component. This is useful when working with typed systems like GraphQL, and you need to avoid unions.

The <ComponentLookup /> component

<ComponentLookup /> simply maps to another component defined in a components object. If you were using it in React, you would use it like:

<ComponentLookup componentType="Button" componentIndex={0} />

which would look up the Button component at index 0 in the components object, resolving to:

<Button id={0}>Hello, World!</Button>

For react-from-json we use JSON, so we would write this:

{
  "type": "ComponentLookup",
  "props": {
    "componentType": "Button",
    "componentIndex": 0
  }
}

The id here is set by the componentIndex, since we didn't specify one in the JSON. See this comment on IDs for more information.

Example

Here's the same example as above, instead using a <ComponentLookup /> for entry.props.patty, and providing a separate components object.

import React from "react";
import ReactFromJSON from "react-from-json";

const entry = {
  type: "Burger",
  props: {
    chain: "Wahlburger",
    patty: {
      type: "ComponentLookup",
      props: {
        componentIndex: 0,
        componentType: "Patty"
      }
    }
  }
};

const mapping = {
  Burger: ({ chain, patty }) => (
    <div>
      <h1>{chain}</h1>
      <div>{patty}</div>
    </div>
  ),
  Patty: ({ variant }) => <span>{variant}</span>
};

const components = {
  Patty: [
    {
      type: "Patty",
      props: {
        variant: "Impossible"
      }
    }
  ]
};

const Example = () => {
  return (
    <ReactFromJSON entry={entry} mapping={mapping} components={components} />
  );
};

A note on ids

react-from-json will map id from the root of your component JSON to the React component's id prop. Likewise, if you specify id under props, it will use this. If you use the <ComponentLookup /> component, react-from-json will use the array index as id unless another id is specified. Your id will always take priority.

With TypeScript

react-from-json supports generic types for use with TypeScript.

import { entry, mapping, components } from "./aboveExample";
import ReactFromJSON from "react-from-json";

interface Components {
  Patty: object[];
}

interface Mapping {
  Burger: React.ReactNode;
  Patty: React.ReactNode;
}

class BurgerReactFromJSON extends ReactFromJSON<Mapping, Components> {
  render(): JSX.Element {
    return super.render();
  }
}

const Example = () => {
  return (
    <BurgerReactFromJSON
      entry={entry}
      mapping={mapping}
      components={components}
    />
  );
};

License

MIT © Measured Co.

react-from-json's People

Contributors

chrisvxd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.