优点&amp; <mycomp>和React.createElement(MyComp)

时间:2017-07-07 11:51:11

标签: reactjs

使用React.createElement的主要思路是什么?

之间的区别:

const myProps = { initialValue: 1 };
const Comp = React.createElement(MyComp, { ...myProps, ref: "mycomp" });

<MyComp ref="mycomp"    initialValue ="1"   />

1 个答案:

答案 0 :(得分:2)

<MyComp ref="mycomp" initialValue ="1" />

转换为:

"use strict";

React.createElement(MyComp, { ref: "mycomp", initialValue: "1" });

没什么不同
const myProps = { initialValue: 1 };
const Comp = React.createElement(MyComp, { ...myProps, ref: "mycomp" });

用法方面的主要差异是非常主观的;有些人发现JSX声明性语法更容易理解,但这超出了SO的范围。