简体   繁体   English

在 React.createElement() 中传递参数

[英]Passing argument inside React.createElement()

I am trying to pass argument name in React.createElement() and I failed.我试图在React.createElement()中传递参数名称,但失败了。 And I want to use JSX only.而且我只想使用 JSX。 Here is my code:这是我的代码:

const Main = (props) => {
    return React.createElement(
    'div',
    null,
    React.createElement('h1', {props.name})
    )
}

The second argument to createElement is the props we are passing to the component and third argument is the children, here props.name needs to be the child of h1 tag createElement 的第二个参数是我们传递给组件的道具,第三个参数是孩子,这里props.name需要是 h1 标签的孩子

React.createElement('h1', null, props.name);

You can verify this from the online babel compiler您可以从在线 babel 编译器验证这一点

Online babel compiler 在线 babel 编译器

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM