Skip to content

Commit

Permalink
Merge pull request #120 from EdonGashi/patch-1
Browse files Browse the repository at this point in the history
Added preloadWeak static method
ScriptedAlchemy authored Jun 4, 2018
2 parents 4fae5eb + 3752c46 commit bf7f1bf
Showing 2 changed files with 33 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -274,9 +274,19 @@ const MyUniversalComponent = universal(import('./MyComponent'))
// call this only after you're sure it has loaded
MyUniversalComponent.doSomething()
// If you are not sure if the component has loaded or rendered, call preloadWeak().
// This will attempt to hoist and return the inner component,
// but only if it can be loaded synchronously, otherwise null will be returned.
const InnerComponent = MyUniversalComponent.preloadWeak()
if (InnerComponent) {
InnerComponent.doSomething()
}
```
> NOTE: for imports using dynamic expressions, conflicting methods will be overwritten by the current component

> NOTE: preloadWeak() will not cause network requests, which means that if the component has not loaded, it will return null. Use it only when you need to retrieve and hoist the wrapped component before rendering. Calling preloadWeak() on your server will ensure that all statics are hoisted properly.

## Props API

- `isLoading: boolean`
25 changes: 23 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -76,11 +76,29 @@ export default function universal<Props: Props>(
return requireAsync(props, context)
})
.then(Component => {
hoist(UniversalComponent, Component, { preload: true })
hoist(UniversalComponent, Component, {
preload: true,
preloadWeak: true
})
return Component
})
}

static preloadWeak(props: Props, context: Object = {}) {
props = props || {}
const { requireSync } = req(component, options, props)

const Component = requireSync(props, context)
if (Component) {
hoist(UniversalComponent, Component, {
preload: true,
preloadWeak: true
})
}

return Component
}

static contextTypes = {
store: PropTypes.object,
report: PropTypes.func
@@ -228,7 +246,10 @@ export default function universal<Props: Props>(
const { Component, error } = state

if (Component && !error) {
hoist(UniversalComponent, Component, { preload: true })
hoist(UniversalComponent, Component, {
preload: true,
preloadWeak: true
})

if (this.props.onAfter) {
const { onAfter } = this.props

0 comments on commit bf7f1bf

Please sign in to comment.