Reactjs Render component dynamically based on a JSON config

前端 未结 2 1949
暖寄归人
暖寄归人 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-14 18:45

    React.renderComponent() has been deprecated, to use React.render() https://facebook.github.io/react/blog/2014/10/28/react-v0.12.html#deprecations

    You may do something like:

    var loadReactModule = function ($root, type, data) {
        var ContentContent= React.createClass({
            render: function () {
                return (
                    
                );
            }
        });
    
        var ContentFormContent= React.createClass({
            render: function () {
                return (
                    
    ); } }); var allComponents = { ContentContent: ContentContent, ContentFormContent: ContentFormContent }; if (type in allComponents) { $root.each(function (index, rootElement) { React.render(React.createElement(allComponents[type]), {data:data}, rootElement); }); } };

提交回复
热议问题