-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle errors of React lifecycle methods (#661)
* handle errors of lifecycle methods * handle errors of render method
- Loading branch information
Showing
4 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
import 'react-hot-loader/patch' | ||
import * as next from './next' | ||
import patch from './patch-react' | ||
|
||
// apply patch first | ||
patch((err) => { | ||
console.error(err) | ||
next.renderError(err) | ||
}) | ||
|
||
const next = require('./next') | ||
window.next = next |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// monkeypatch React for fixing https://github.com/facebook/react/issues/2461 | ||
// based on https://gist.github.com/Aldredcz/4d63b0a9049b00f54439f8780be7f0d8 | ||
|
||
import React from 'react' | ||
|
||
let patched = false | ||
|
||
export default (handleError = () => {}) => { | ||
if (patched) { | ||
throw new Error('React is already monkeypatched') | ||
} | ||
|
||
patched = true | ||
|
||
const { createElement } = React | ||
|
||
React.createElement = function (Component, ...rest) { | ||
if (typeof Component === 'function') { | ||
const { prototype } = Component | ||
if (prototype && prototype.render) { | ||
prototype.render = wrapRender(prototype.render) | ||
} else { | ||
// stateless component | ||
Component = wrapRender(Component) | ||
} | ||
} | ||
|
||
return createElement.call(this, Component, ...rest) | ||
} | ||
|
||
const { Component: { prototype: componentPrototype } } = React | ||
const { forceUpdate } = componentPrototype | ||
|
||
componentPrototype.forceUpdate = function (...args) { | ||
if (this.render) { | ||
this.render = wrapRender(this.render) | ||
} | ||
return forceUpdate.apply(this, args) | ||
} | ||
|
||
function wrapRender (render) { | ||
if (render.__wrapped) { | ||
return render.__wrapped | ||
} | ||
|
||
const _render = function (...args) { | ||
try { | ||
return render.apply(this, args) | ||
} catch (err) { | ||
handleError(err) | ||
return null | ||
} | ||
} | ||
|
||
// copy all properties | ||
Object.assign(_render, render) | ||
|
||
render.__wrapped = _render.__wrapped = _render | ||
|
||
return _render | ||
} | ||
} |
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