I have a React project bundled with Webpack.
I have a component that I want it to display components dynamically. In my case, the component path comes from props.
Also, these components are not related in my project .js file. they are external components of React / libaries.
I tried importing Dynamic ES6:
componentWillReceiveProps(nextProps){ if(nextProps.pagesData && this.props.pagesData !== nextProps.pagesData && nextProps.pagesData.get('cards').count() > 0){ // Getting the first card from the Immutable object let card = nextProps.pagesData.getIn(['cards', 0]); // Getting the cardType which can be: '/path/index.js' let cardType = card.get('card_type'); // ES6 Dynamic import import(cardType) .then(module => { this.setState({ asyncCard: module.default }); }) } }
This does not work, because import needs a static route.
Then I tried with the requirement:
let dynamicComponent = require(cardType);
Which does not work because (I suppose) Webpack is trying to find it in the main bundle.
Is it possible to do this?
Update : it looks like this might work (cardType is index.js is a React component):
import(`/home/user/_apps/module/react-module/lib/${cardType}`)
Webpack creates another package (chunk), including the index.js code and all its dependencies.
But this really does not solve my initial question.
Change 2 . The import from above actually ignores the last var and Webpack makes chunks of each file inside / lib.