Posts

Showing posts with the label Create React App

Conditionally Import Assets In Create-react-app

Answer : In the days when React didn't exist we didn't put assets into our JS files. We let the CSS to decide, what assets to load for what selectors. Then you could simply switch a corresponding class on or off for a corresponding element ( or even the whole page ) and viola it changes color, background, or even a form. Pure magic! Ah. What times these were! All above is true and I do not understand why would anyone do or recommend doing it differently. However if you still want to do it ( for any reason ) - you can! Latest create-react-app comes with out-of-the-box support for lazy loading of arbitrary components via dynamic importing and code splitting. All you need to do is use parenthesized version of the import() statement instead of the regular one. import() takes in a request string as usual and returns a Promise. That's it. Source code of the dynamicaly requested component won't be bundled in, but instead stored in separate chunks to be loaded on demand. ...