写在前面:在React中使用JSX不是必须的,如果不想在build环境中添加解析器,React without JSX是很方便的
JSX的只是React的语法糖,用来代替
React.createElement(component, props, …children)
举例来说:
使用JSX
return (
<div>Hello {this.props.test}</div>
)
不使用JSX
return (
React.createElement('div',null,`Hello ${this.props.test}`)
)
使用JSX
<Hello test="test"></Hello>
不使用JSX
React.createElement(Hello,{test: "test"},null)
写在最后:
多次使用React.createElement时,可以使用e来替代使用。 const e = React.createElement;