Comments (3)
No problem 😄 It's a good question.
Dan pointed out that the react-lifecycles-compat
package is primarily intended for library authors (so they can support multiple React versions downstream), and we don't expect it to be common for libraries to define their own error boundaries. (Error boundaries are lightweight and typically application-specific.)
So I think we can close this issue as a non-goal for now!
from react-lifecycles-compat.
Error boundaries (introduced in v16.0 with componentDidCatch
) are not something that can be polyfilled for older versions of React. They're a totally new behavior and not something that could be simulated using pre-existing lifecycles.
I believe it would be possible to polyfill getDerivedStateFromError
for v16.0.x-16.5.x using componentDidCatch
but I would need to think about it more to be sure.
That being said, it is not as important to polyfill getDerivedStateFromError
as it was for getDerivedStateFromProps
and getSnapshotBeforeUpdate
. Those lifecycles came with deprecation warnings so it was important that we provided library authors with some way of upgrading their components in order to support both older and newer versions of React without cluttering the console with dev warnings.
from react-lifecycles-compat.
Hey Brian -- thanks for the quick response!
This is not something I need, but rather was curious about when trying to summarize some highlights from the v16 changelog. It makes sense that the behaviour isn't possible in earlier versions, and agree it isn't as important to back-port the new functionality.
Just for interest, I tried to figure out how the lib worked for the other lifecycles method and arrived at this analogy for getDerivedStateFromError
:
export function polyfillDerivedStateFromError(Component){
var prototype = Component.prototype;
if (!prototype || !prototype.isReactComponent) {
throw new Error('Can only polyfill class components');
}
if (
typeof Component.getDerivedStateFromError !== 'function'
) {
return Component;
}
if (typeof prototype.getDerivedStateFromError === 'function') {
if (typeof prototype.componentDidCatch !== 'function') {
throw new Error(
'Cannot polyfill getDerivedStateFromError() for components that do not define componentDidCatch() on the prototype'
);
}
var componentDidCatch = prototype.componentDidCatch;
prototype.componentDidCatch = function componentDidCatchPolyfill(
error,
info
) {
var stateChange = this.getDerivedStateFromError(error)
this.setState(stateChange); // Note below
componentDidCatch.call(this, error, info);
};
}
}
I don't think the bound updater
function like we see with the polyfilled gDSRP is necessary because prevState
isn't needed for gDSFE, but I could be misinterpreting the intent there.
But, as you pointed out, this only works for 16.0-16.5 anyways due to reliance on cDC, and there should not be any major impediment to updating to 16.6 if already on 16.0+.
Feel free to close this if it's not otherwise a goal.
✌️
from react-lifecycles-compat.
Related Issues (20)
- Doesn't demand `null` be returned HOT 5
- Add changelog HOT 2
- Template strings are not supported on old browsers HOT 1
- Use react-lifecycles-compat with hoc HOT 5
- Failed to compile with Create-react-app. HOT 2
- Perhaps fill that ellipsis in the README with a couple of methods. HOT 1
- Package is not published pre-compiled HOT 6
- StrictMode warnings HOT 2
- Failed to minify the code from this file: react-lifecycles-compat.es.js:64 HOT 8
- IE 10 support HOT 5
- v1.1.4 suddenly not exporting module? HOT 1
- `getDerivedStateFromProps` not called before each `render` HOT 1
- object doesn't support property or method 'getDerivedStateFromProps'
- support UNSAFE_componentWillUnmount HOT 4
- License not version-tagged
- Test case failure while building react-lifecycles-compat on PowerPC64LE - ppc64le
- 1.1.1 error __webpack_require___(...) is not a function HOT 10
- Use named export and add ES module to dist HOT 2
- getSnapshotBeforeUpdate polyfill incompatible with version 16.3 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-lifecycles-compat.