Reactjs Render component dynamically based on a JSON config

前端 未结 2 1958
暖寄归人
暖寄归人 2021-02-14 18:06

I have a following config as JSON

var componentConfig = {
content: { type: \"ContentContent\", data: \"content\"},
new_content: { type: \"ContentFormContent\", d         


        
2条回答
  •  Happy的楠姐
    2021-02-14 18:59

    The JSX

    
    

    simply compiles to

    ContentFormContent({data: [componentConfig.new_content.data]})
    

    so you can make that function call however you like. In this case, it's probably most convenient to make a list of all possible components and do something like

    var allComponents = {
        ContentContent: ContentContent,
        ContentFormContent: ContentFormContent
    };
    
    // (later...)
    React.renderComponent(allComponents[component.type]({data: component.data}), body);
    

    if component is an element from your example array.

提交回复
热议问题