如何使用 `React.createElement` 子参数(无 jsx)
How to use `React.createElement` children parameter (without jsx)
React.createElement
需要传播 "children" 参数
var d = React.DOM;
React.createElement(LabeledElement, {label: "Foo"},
d.input({value: "foo"})
)
但我找不到任何关于如何实际使用它的文档
var LabeledElement = React.createClass({
render: function() {
return d.label({}
,d.span({classNames: 'label'}, this.props.label)
, //How to place children here?
}
})
我确定这有一个非常非常简单的答案。
通过 JSX 嵌套或通过 React.createElement
的第三个以上参数传递给组件的子项在组件中显示为 this.props.children
:
var MyLabel = React.createClass({
render: function() {
return React.createElement("label", {className: "label"},
React.createElement("span", {className: "label"}, this.props.label),
this.props.children
);
}
});
var App = React.createClass({
render: function() {
return React.createElement(MyLabel, {label: "Here is the label prop"},
React.createElement("div", {},
React.createElement("input", {type: "text", value: "And here is a child"})
)
);
}
});
示例:http://jsfiddle.net/BinaryMuse/typ1f2mf/; docs: http://facebook.github.io/react/docs/multiple-components.html#children
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
var MyLabel = React.createClass({
render: function() {
return React.createElement("label", {className: "label"},
React.createElement("span", {className: "label"}, this.props.label),
this.props.children
);
}
});
var App = React.createClass({
render: function() {
return React.createElement(MyLabel, {label: "Here is the label prop"},
React.createElement("div", {},
React.createElement("input", {type: "text", value: "And here is a child"})
)
);
}
});
React.createElement
需要传播 "children" 参数
var d = React.DOM;
React.createElement(LabeledElement, {label: "Foo"},
d.input({value: "foo"})
)
但我找不到任何关于如何实际使用它的文档
var LabeledElement = React.createClass({
render: function() {
return d.label({}
,d.span({classNames: 'label'}, this.props.label)
, //How to place children here?
}
})
我确定这有一个非常非常简单的答案。
通过 JSX 嵌套或通过 React.createElement
的第三个以上参数传递给组件的子项在组件中显示为 this.props.children
:
var MyLabel = React.createClass({
render: function() {
return React.createElement("label", {className: "label"},
React.createElement("span", {className: "label"}, this.props.label),
this.props.children
);
}
});
var App = React.createClass({
render: function() {
return React.createElement(MyLabel, {label: "Here is the label prop"},
React.createElement("div", {},
React.createElement("input", {type: "text", value: "And here is a child"})
)
);
}
});
示例:http://jsfiddle.net/BinaryMuse/typ1f2mf/; docs: http://facebook.github.io/react/docs/multiple-components.html#children
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
var MyLabel = React.createClass({
render: function() {
return React.createElement("label", {className: "label"},
React.createElement("span", {className: "label"}, this.props.label),
this.props.children
);
}
});
var App = React.createClass({
render: function() {
return React.createElement(MyLabel, {label: "Here is the label prop"},
React.createElement("div", {},
React.createElement("input", {type: "text", value: "And here is a child"})
)
);
}
});