Last active
March 11, 2018 07:35
-
-
Save mohandere/f49f49f0b7cc6d4ba0b74eaa2d330f5a to your computer and use it in GitHub Desktop.
Code Splitting - dynamic import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
class App extends Component { | |
handleClick = () => { | |
import ('./moduleA') | |
.then(({ | |
moduleA | |
}) => { | |
// Use moduleA | |
}) | |
.catch(err => { | |
// Handle failure | |
}); | |
}; | |
render() { | |
return ( | |
<div> | |
<button onClick={this.handleClick}>Load</button> | |
</div> | |
); | |
} | |
} | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dynamic import example | |
// Before | |
import { add } from './math'; | |
console.log(add(16, 26)); | |
// After | |
import("./math").then(math => { | |
console.log(math.add(16, 26)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment