Dynamically load and render react component with input[type=file]

Adam

I'd like to load a react js component dynamically through an html file input. Essentially, I'd like to achieve the same effect as if I had done import Foo from ./foo.js

I can read the file as text after the onChange event of the html element, but then I don't know what to do with it? Is it even possible to achieve my goal? Thanks!

onChange(e) {
  var fr = new FileReader()

  fr.addEventListener('load', f => {
    window.console.log(f.target.result.substring(0, 500))
    // yields: import React, { Component } from 'react' ... class Foo extends Component { ...
    // but now what?
  })

  fr.readAsText(e.target.files[0])
}
Adam

So, I ended up getting this to work with babel-standalone. I'm only able to use stateless, functional components, however, and I feel as though my implementation could be improved. For example, I don't like how I have to string.prototype.replace.

Anyway, here's what worked for me, hopefully it might help you, too.

// fileToBeLoaded.js
const elem = () => <h1>Hello, world!</h1>

// foo.js
import React, { Component } from 'react'
import { transform } from 'babel-standalone'

class Foo extends Component {
    constructor(props) {
        super(props)

        this.state = {
            elem: undefined
        }

        this.handleChange = this.handleChange.bind(this)
    }

    handleChange(e) {
        let fr = new FileReader()

        fr.addEventListener('load', e1 => {
            let s = e1.target.result
            let result = transform(s, {
                presets: ['es2015'],
                plugins: ['transform-react-jsx']
             })

             let s1 = result.replace(`'use strict';

             let elem = function elem(props) {`, 'elem = function(props) {')

             let elem = () => {}
             elem = eval(s1)

             this.setState({elem})
        })

        fr.readAsText(e.target.files[0])
    }

    render() {
        const Elem = this.state.elem ? this.state.elem : () => <h1>No elem!</h1>
        return (
            <div>
                <input type="file" onChange={this.handleChange}
                <Elem />
            </div>
        )
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically render a component inside a another component in React

From Dev

How to dynamically load & render react components?

From Dev

Render a single react component in a html file (webpack)

From Dev

Is there a way to dynamically load pure HTML in React as a separate component?

From Dev

How to load react component dynamically using require with babel-register

From Dev

React render nested component

From Dev

React component does not render

From Dev

Render React Component to Fancybox

From Dev

REACT - Error to render component

From Dev

Render undefined in react component?

From Java

Dynamically Rendering a React component

From Dev

Create react component dynamically

From Dev

Load a Component in custom element dynamically

From Dev

How to load Component dynamically in reactjs?

From Dev

dynamically load resource file

From Java

Dynamically building a render function in React

From Dev

Reactjs Render component dynamically based on a JSON config

From Dev

how do I dynamically render title as component

From Dev

How to dynamically render JSX component X times?

From Dev

How to animate a React component on render?

From Dev

Render a component within another in React

From Dev

React: how to render data into a component?

From Dev

Render react component multiple times

From Dev

Render react component with twig conditions

From Dev

Render email message to component in React

From Dev

React Redux render connected component

From Dev

Component won't render in react

From Dev

ReactJS - Render top level component on load

From Dev

React component with dynamically created function