加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 如何使用`React.createElement` children参数(不带js

发布时间:2020-12-15 20:52:25 所属栏目:百科 来源:网络整理
导读:React.createElement传递一个“children”参数 var d = React.DOM;React.createElement(LabeledElement,{label: "Foo"},d.input({value: "foo"})) 但是我找不到任何关于如何实际使用它的文档 var LabeledElement = React.createClass({ render: function() {
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",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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读