Search code examples
javascriptreactjsreactjs-flux

Dynamically inserted React Components


I'm building a WYSIWYG tool in React/flux where you build interfaces by dragging and dropping elements onto a page. So for instance, I have a list of droppable elements: List, Form, Input Box, Text Box, etc. When the user drops one of those elements on the page, I need to insert the associated react component (and I store a name property of the object they drop, so I will know the component name I need). So if a user drops a list onto the page, and my associated component tag is <list />, I can't do just <{component.name} /> and it will turn it into <list />...yet I need a method of doing so. Any thoughts?


Solution

  • So fiddling around with this, I ended up getting it to work like this:

    var Sort = require('./sort')
    var List = require(./list')
    
    var allComponents = {
        sort: Sort,
        list: List
    }
    
    React.createElement(allComponents[component.componentName], {data:component.data})
    

    so now if my component.componentName is list, it will render the component, and data will become my properties