gpt4 book ai didi

reactjs - 如何使用 `React.createElement` 子参数(不带 jsx)

转载 作者:行者123 更新时间:2023-12-03 12:58:08 24 4
gpt4 key购买 nike

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/ ;文档:http://facebook.github.io/react/docs/multiple-components.html#children

关于reactjs - 如何使用 `React.createElement` 子参数(不带 jsx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com