使用React.createElement的主要思路是什么?
之间的区别:
const myProps = { initialValue: 1 };
const Comp = React.createElement(MyComp, { ...myProps, ref: "mycomp" });
和
<MyComp ref="mycomp" initialValue ="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的范围。