Dynamic Require in ReactJS/NextJS -- componentWillMount

Viewed 589

I am using ReactJS/NextJS and need to "require" nodejs modules (different themes) depending on the Component's this.props.theme category. I do not want to "require" all the themes (js files) unnecessarily because I think that would cause unnecessary load time.

I came up with the following solution, but I've since realized that ComponentWillMount/ComponentWillUpdate are deprecated. How would I write this up to date?:

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)
}

What should I do instead for these dynamic imports?

0 Answers
Related