I am working on updating react version to 18.2 from 16.8.6 in one of my projects. After doing the upgrade one of the major warnings that I got in console is regarding the deprecated lifecycle methods like componentWillReceiveProps
.
The warning given by react is Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 18.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run
npx react-codemod rename-unsafe-lifecycles in your project source folder.
.
But componentWillReceiveProps
is still working for me. Did I do something wrong while upgrading, or is it supposed to work?
I just bootstrapped a fresh React v18 project and used componentWillReceiveProps
. It worked, and I got the same "In React 18.x, only the UNSAFE_ name will work..." text you did. So it's still there, even in a fresh project.
In a future major version of React, only the new name will work.
Apparently the warning text is somewhat out of date and the removal of the old name hasn't been done yet.
That said, it's probably best to at least rename the method in you code at this point (perhaps with the codemod they recommend), and ideally refactor to stop using it entirely.