react-image-fallback
react-image-fallback copied to clipboard
React lifecycle method warnings
React deprecates componentWillReceiveProps
in favor of getDerivedStateFromProps
. Documentation. If we don’t fix this, the component will be broken in React 17.
https://github.com/socialtables/react-image-fallback/blob/6adce1ac5e5925f86d7b9ecf6099f8ca94a53607/src/index.js#L39-L47
While we wait for this package to be updated, this works for me for now:
import ReactImageFallback from 'react-image-fallback'
const UNSAFE_LIFECYCLE_METHOD_NAMES = [
'componentWillMount',
'componentWillReceiveProps',
'componentWillUpdate',
]
function renameUnsafeLifecycleMethods (Component) {
const { prototype } = Component
UNSAFE_LIFECYCLE_METHOD_NAMES.forEach((methodName) => {
if (!prototype[methodName]) return
Object.defineProperty(prototype, `UNSAFE_${methodName}`, {
configurable: true,
value: prototype[methodName],
writable: true,
})
Reflect.deleteProperty(prototype, methodName)
})
return Component
}
const ImageFallback = renameUnsafeLifecycleMethods(ReactImageFallback)
Just created a PR for this issue: https://github.com/socialtables/react-image-fallback/pull/59