我刚刚用 react-js 弄脏了我的手,并且遇到了这段代码,用于在我的应用程序中动态导入我似乎无法理解的组件?
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Dynamic extends Component {
constructor(props) {
super(props);
this.state = { module: null };
}
componentDidMount() {
const { path } = this.props;
import(`${path}`)
.then(module => this.setState({ module: module.default }))
}
render() {
const { module: Component } = this.state; // Assigning to new variable names @see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
return(
<div>
{Component && <Component />} // the code i can't figure out
//{Component} works fine too
//{<Component />} gives error
</div>
)
}
}
ReactDOM.render(<Dynamic path='./FirstComponent' />, document.getElementById('root'));
有人可以解释一下我突出显示的代码行吗{组件} 也是?