MCPcopy Index your code
hub / github.com/ctrlplusb/react-async-component / asyncComponent

Function asyncComponent

src/asyncComponent.js:6–203  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

4const validSSRModes = ['resolve', 'defer', 'boundary']
5
6export default function asyncComponent(config) {
7 const {
8 name,
9 resolve,
10 autoResolveES2015Default = true,
11 serverMode = 'resolve',
12 LoadingComponent,
13 ErrorComponent,
14 } = config
15
16 if (validSSRModes.indexOf(serverMode) === -1) {
17 throw new Error('Invalid serverMode provided to asyncComponent')
18 }
19
20 const env =
21 ['node', 'browser'].indexOf(config.env) > -1
22 ? config.env
23 : typeof window === 'undefined' ? 'node' : 'browser'
24
25 const state = {
26 // A unique id we will assign to our async component which is especially
27 // useful when rehydrating server side rendered async components.
28 id: null,
29 // This will be use to hold the resolved module allowing sharing across
30 // instances.
31 // NOTE: When using React Hot Loader this reference will become null.
32 module: null,
33 // If an error occurred during a resolution it will be stored here.
34 error: null,
35 // Allows us to share the resolver promise across instances.
36 resolver: null,
37 // Indicates whether resolving is taking place
38 resolving: false,
39 // Handle on the contexts so we don't lose it during async resolution
40 asyncComponents: null,
41 asyncComponentsAncestor: null,
42 }
43
44 const needToResolveOnBrowser = () =>
45 state.module == null &&
46 state.error == null &&
47 !state.resolving &&
48 typeof window !== 'undefined'
49
50 // Takes the given module and if it has a ".default" the ".default" will
51 // be returned. i.e. handy when you could be dealing with es6 imports.
52 const es6Resolve = x =>
53 autoResolveES2015Default &&
54 x != null &&
55 (typeof x === 'function' || typeof x === 'object') &&
56 x.default
57 ? x.default
58 : x
59
60 const getResolver = () => {
61 if (state.resolver == null) {
62 state.resolving = true
63 try {

Callers 3

createAppFunction · 0.85

Calls

no outgoing calls

Tested by 1

createAppFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…