本文介绍了动态创建反应组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试根据与数据一起传递的参数呈现不同的组件。
如果我使用常规或React.createElement(Item) - 它工作正常,但所有其他选项都失败了。

Trying to render different components depending on arguments passed with data.If I use regular or React.createElement(Item) - it works fine, but all other options fail.

var React = window.React;

var data = {items: [
{ itemClass: 'Item', id: 1, contentsHTML: '', text: 'Item 1'},
{ itemClass: 'Item', id: 2, contentsHTML: '', text: 'Item 2'},
{ itemClass: 'Item', id: 3, contentsHTML: '', text: 'Item 3'},
{ itemClass: 'Item', id: 4, contentsHTML: '', text: 'Item 4'},
{ itemClass: 'Item', id: 5, contentsHTML: '', text: 'Item 5'}
]};

var MyCatalog = React.createClass({
  getInitialState: function() {
    return { data: { items: [] } };
  },
  componentDidMount: function () {
    this.setState({ data: this.props.data });
  },

  render: function () {
    return (
      <div className="catalog">
        HELLO!!! I AM A CATALOG!!!

        <ItemList data={this.state.data}/>
      </div>
    );
  }
});

var ItemList = React.createClass({
  render: function () {
    console.log(this.props);

    var items = this.props.data["items"].map(function (itemData) {

    var klass = itemData['itemClass'] || 'Item';
    var ItemFactory = React.createFactory(klass);

    //return <ItemFactory key={itemData['id']} data={itemData}/> // no
    //return <klass key={itemData['id']} data={itemData}/> // no
    //return React.createElement(klass, { key: itemData['id'], data: itemData }); // no
    //return <Item data={itemData}/> // ok
    //return React.createElement(Item, { data: itemData }); // ok
    //return React.createElement('Item', { key: itemData['id'], data: itemData }); // no
    //return React.createElement(React.createFactory('Item'), { data: itemData }); // no, error
    var component = Components['itemClass'];
    return <component data={itemData} key={itemData['id']}/>
  });

  console.log(items);
  return (
    React.createElement('div', { className: 'list' },
      React.createElement('div', null, 'And I am an ItemList :'),
        React.createElement('div', null, items)
      )

      /*<div className="list">
      <div>And I am an ItemList :</div>
      <div>
        {items}
      </div>
      </div>*/
    );
  }
});

var Item = window.Item = React.createClass({
  render: function () {
    return (
      <div className="item">
        <div>
          Regular item. Nothing special.
        </div>
          {this.props.children}
        </div>
      );
    }
  });

  var Components = { 'Item': Item };

  React.render(
    <MyCatalog data={data}/>,
    document.getElementById('app')
  );

如何针对不同类型的组件管理此案例?

How can I manage this case for different types of components?

推荐答案

这应该有效:

var component = Components[itemData['itemClass']]);
return React.createElement(component, {
    data: itemData,
    key: itemData['id']
});

你不能像这样使用JSX语法< component ... /> 因为当它被转换成组件时不会引用任何内容。

You can't use JSX syntax like this <component .../> because when it gets transpiled component won't refer to anything.

更新:这是更新的ItemList组件:

UPDATE: Here is the updated ItemList component in full:

var ItemList = React.createClass({
    render: function() {
        console.log(this.props);

        var items = this.props.data["items"].map(function(itemData) {
            var component = Components[itemData['itemClass']];
            return React.createElement(component, {
                data: itemData,
                key: itemData['id']
            });
        });
        console.log(items);
        return (
            <div className="list">
                <div>And I am an ItemList</div>
                <div>{items}</div>
            </div>
        );
    }
});

你可以看到它在这个小提琴中起作用:

You can see it working in this fiddle: http://jsfiddle.net/fmhhtk5o/3/

这篇关于动态创建反应组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:24