Saturday 27 May 2017

render React component from a string

I have some React code in the string, for example:

const component = `
function App() {
  return (
    <div>
    test
    </div>
  );
}
`;

And I want to be able to render that component from within browser, something like:

import React, { Component } from 'react';
import { render } from 'react-dom';
import * as babel from 'babel-standalone';


const babelCode = babel.transform(component, { presets: ['react', 'es2015'] }).code;

render(eval(babelCode), document.getElementById('WorkFlow'));

This particular example doesn't work but it shows what I'm looking for, any help appreciated!

Thanks!



via Sarkis Arutiunian

No comments:

Post a Comment