react-image-fallback icon indicating copy to clipboard operation
react-image-fallback copied to clipboard

React lifecycle method warnings

Open denis-sokolov opened this issue 6 years ago • 2 comments

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

denis-sokolov avatar Oct 24 '18 07:10 denis-sokolov

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)

elquimista avatar Aug 23 '19 13:08 elquimista

Just created a PR for this issue: https://github.com/socialtables/react-image-fallback/pull/59

martinfoakes avatar May 04 '21 10:05 martinfoakes