ES6: Conditional & Dynamic Import Statements
We do have dynamic imports proposal now with ECMA. This is in stage 2. This is also available as babel-preset.
Following is way to do conditional rendering as per your case.
if (foo === bar) {
import('./Baz')
.then((Baz) => {
console.log(Baz.Baz);
});
}
This basically returns a promise. Resolution of promise is expected to have the module. The proposal also has things like multiple dynamic imports, default imports, js file import etc. You can find more information about dynamic imports here.
How can I conditionally import an ES6 module?
We do have dynamic imports proposal now with ECMA. This is in stage 3. This is also available as babel-preset.
Following is way to do conditional rendering as per your case.
if (condition) {
import('something')
.then((something) => {
console.log(something.something);
});
}
This basically returns a promise. Resolution of promise is expected to have the module. The proposal also have other features like multiple dynamic imports, default imports, js file import etc. You can find more information about dynamic imports here.
Related Topics
Why Do We Need to Install Gulp Globally and Locally
Limit Concurrency of Promise Being Run
Semicolon Before Self-Invoking Function
React - Preventing Form Submission
This' Does Not Work Properly in Another Event. I'm Clueless as to Why
How to Pass Data to Url from Jqgrid Row If Hyperlink Is Clicked
How to Access a Child's State in React
Window.Print() Not Working in Ie
JavaScript Can't Find Element by Id
How to Print a Stack Trace in Node.Js
How to Create a Web Worker from a String
What Is Difference Between Axios and Fetch
Method for Streaming Data from Browser to Server via Http
Var Name Produces Strange Result in JavaScript
Check Whether an Array Exists in an Array of Arrays