Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js ReactJS/NextJS中的动态需求——组件将挂载_Node.js_Reactjs - Fatal编程技术网

Node.js ReactJS/NextJS中的动态需求——组件将挂载

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(

我使用的是ReactJS/NextJS,需要根据组件的this.props.theme类别“需要”节点JS模块(不同的主题)。我不想不必要地“要求”所有主题(js文件),因为我认为这会导致不必要的加载时间

我提出了以下解决方案,但后来我意识到ComponentWillMount/ComponentWillUpdate已被弃用。我该如何将其更新

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());组件的呈现部分取决于“必需/导入”的主题。基本上,在加载所需路径之前,我不希望渲染部分运行。