在ReactNative中,您似乎可以做一些类似于new SomeComponent(props)和return someComponent.render()的事情。我猜这基本上就是JSX在直接声明<SomeComponent someProp="etc"/>时所做的事情吧?如果是这样的话,是否有任何潜在的不利因素手动操作组件而不是使用JSX?
发布于 2018-01-31 19:38:06
在ReactNative中,您似乎可以执行类似于新SomeComponent(props)的操作,并返回someComponent.render()
不怎么有意思。如果手动实例化组件类,除其他外,生命周期方法(componentWillMount()、componentDidMount()将无法工作。
我猜这基本上就是JSX在直接声明
看看babel编译到什么,但基本上JSX编译成普通的js:
class Hello extends React.Component {
render() {
return <div>Hello {this.props.toWhat}</div>;
}
}翻译为:
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
}
}如果是这样的话,是否存在手动操作组件而不是使用JSX的潜在缺点?
您可以使用React.createElement,完全避免使用JSX,但在我的经验中,这更难、更详细。手动实例化组件,虽然正如您最初建议的那样,但它违背了使用React的目的:)
一些更详细的好链接:
https://stackoverflow.com/questions/48549901
复制相似问题