Unverified Commit 139da5ad authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Prevent extraneous re-renders with next/dynamic (#11587)

* Prevent extraneous rerenders with loadables

* Don't try to spread undefined

* Allow ref to be memoized
parent 5db180b2
...@@ -164,9 +164,13 @@ function createLoadableComponent(loadFn, options) { ...@@ -164,9 +164,13 @@ function createLoadableComponent(loadFn, options) {
const context = React.useContext(LoadableContext) const context = React.useContext(LoadableContext)
const state = useSubscription(subscription) const state = useSubscription(subscription)
React.useImperativeHandle(ref, () => ({ React.useImperativeHandle(
retry: subscription.retry, ref,
})) () => ({
retry: subscription.retry,
}),
[]
)
if (context && Array.isArray(opts.modules)) { if (context && Array.isArray(opts.modules)) {
opts.modules.forEach(moduleName => { opts.modules.forEach(moduleName => {
...@@ -174,19 +178,21 @@ function createLoadableComponent(loadFn, options) { ...@@ -174,19 +178,21 @@ function createLoadableComponent(loadFn, options) {
}) })
} }
if (state.loading || state.error) { return React.useMemo(() => {
return React.createElement(opts.loading, { if (state.loading || state.error) {
isLoading: state.loading, return React.createElement(opts.loading, {
pastDelay: state.pastDelay, isLoading: state.loading,
timedOut: state.timedOut, pastDelay: state.pastDelay,
error: state.error, timedOut: state.timedOut,
retry: subscription.retry, error: state.error,
}) retry: subscription.retry,
} else if (state.loaded) { })
return opts.render(state.loaded, props) } else if (state.loaded) {
} else { return opts.render(state.loaded, props)
return null } else {
} return null
}
}, [props, state])
} }
LoadableComponent.preload = () => init() LoadableComponent.preload = () => init()
...@@ -243,12 +249,12 @@ class LoadableSubscription { ...@@ -243,12 +249,12 @@ class LoadableSubscription {
this._res.promise this._res.promise
.then(() => { .then(() => {
this._update() this._update({})
this._clearTimeouts() this._clearTimeouts()
}) })
// eslint-disable-next-line handle-callback-err // eslint-disable-next-line handle-callback-err
.catch(err => { .catch(err => {
this._update() this._update({})
this._clearTimeouts() this._clearTimeouts()
}) })
this._update({}) this._update({})
...@@ -257,6 +263,9 @@ class LoadableSubscription { ...@@ -257,6 +263,9 @@ class LoadableSubscription {
_update(partial) { _update(partial) {
this._state = { this._state = {
...this._state, ...this._state,
error: this._res.error,
loaded: this._res.loaded,
loading: this._res.loading,
...partial, ...partial,
} }
this._callbacks.forEach(callback => callback()) this._callbacks.forEach(callback => callback())
...@@ -268,12 +277,7 @@ class LoadableSubscription { ...@@ -268,12 +277,7 @@ class LoadableSubscription {
} }
getCurrentValue() { getCurrentValue() {
return { return this._state
...this._state,
error: this._res.error,
loaded: this._res.loaded,
loading: this._res.loading,
}
} }
subscribe(callback) { subscribe(callback) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment