您可以通过道具将组件传递给孩子吗

var TaskForm = require('../../../../components/assessment/tasks/TaskForm.react');

render: function() {
    return (
        <div>
            <div className="row">
                <div className="col-xs-12 col-lg-12">
                    <GroupsItemsAccordionWrapper
                         TaskForm=TaskForm
                    />
                </div>
            </div>
        </div>
    );
}

最佳答案

是的,您可以将任何值作为prop传递,包括React.createClass(也称为组件类)返回的对象。假设您正在实现依赖项注入:

var TaskForm = React.createClass({
  render: function() {
    return (
        <div>{this.props.children}</div>
    );
  }
});

var GroupsItemsAccordionWrapper = React.createClass({
  render: function() {
    var TaskForm = this.props.TaskForm;
    return (
        <div>
          <TaskForm>Hello, world!</TaskForm>
        </div>
    );
  }
});

ReactDOM.render(
  <GroupsItemsAccordionWrapper TaskForm={TaskForm} />,
  document.getElementById('container')
);


(请注意TaskForm={TaskForm}周围的大括号)。

演示:https://jsfiddle.net/69z2wepo/37477/

07-24 14:23