0.1.0 • Published 7 years ago

jsonmltoreact v0.1.0

Weekly downloads
234
License
MIT
Repository
github
Last release
7 years ago

Code Climate Test Coverage

jsonmltoreact

JsonML to React converter

Install

$ npm i --save jsonmltoreact

Examples

import _ from 'lodash';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import JsonmlToReact from 'jsonmltoreact';

// Some random React component
class CustomCompoenent extends React.Component {
  render() {
    return (
      <div className="customCompoenentContainer">
        {this.props.header && <h1>{this.props.header}</h1>}
        {this.props.subheader && <h2>{this.props.subheader}</h2>}
        {this.props.children}
      </div>
    );
  }
}

// Create instance with custom converters
let jsonmlToReact = new JsonmlToReact({
  'custom-tag': (props, data) => ({
    type: CustomCompoenent,
    props: { ...props, ...data },
  }),
  'span': props => ({
    props: {
      className: 'foobar'
    }
  })
});

// JsonML tree
let jsonml = [
  'div', { class: 'container' },
  [
    'custom-tag', { subheader: 'bar' },
    [
      'span', 'content'
    ]
  ]
];

// Optional data to be passed to converters
let data = {
  'header': 'foo',
};

let reactComponent = jsonmlToReact.convert(jsonml, data);

console.log(ReactDOMServer.renderToStaticMarkup(reactComponent));
/*
<div class="container">
  <div class="customCompoenentContainer">
    <h1>foo</h1>
    <h2>bar</h2>
    <span class="foobar">content</span>
  </div>
</div>
*/
0.1.0

7 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago