Node.js ReactJS/NextJS中的动态需求——组件将挂载
我使用的是ReactJS/NextJS,需要根据组件的this.props.theme类别“需要”节点JS模块(不同的主题)。我不想不必要地“要求”所有主题(js文件),因为我认为这会导致不必要的加载时间 我提出了以下解决方案,但后来我意识到ComponentWillMount/ComponentWillUpdate已被弃用。我该如何将其更新Node.js ReactJS/NextJS中的动态需求——组件将挂载,node.js,reactjs,Node.js,Reactjs,我使用的是ReactJS/NextJS,需要根据组件的this.props.theme类别“需要”节点JS模块(不同的主题)。我不想不必要地“要求”所有主题(js文件),因为我认为这会导致不必要的加载时间 我提出了以下解决方案,但后来我意识到ComponentWillMount/ComponentWillUpdate已被弃用。我该如何将其更新 class MyPage extends React.Component { constructor(props){ super(
class MyPage extends React.Component {
constructor(props){
super(props)
}
render() {
<div theme={this.props.theme}/> // theme is a reference to require
}
}
componentWillUpdateProps() {
require("/path/to/theme-" + this.props.theme)
}
componentWillMount() {
require("/path/to/theme-" + this.props.theme)
}
类MyPage扩展了React.Component{
建造师(道具){
超级(道具)
}
render(){
//主题是对需求的参考
}
}
componentWillUpdateProps(){
require(“/path/to/theme-”+this.props.theme)
}
组件willmount(){
require(“/path/to/theme-”+this.props.theme)
}
对于这些动态导入,我应该怎么做?您可以在componentDidMount导入('./Module')中使用动态导入。然后(Module=>Module.method());组件的呈现部分取决于“必需/导入”的主题。基本上,在加载所需路径之前,我不希望渲染部分运行。