I'm using Material ui SwipeableViews That use ReactSwipableView package, I'm getting this error on the console
react-dom.development.js:12466 Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See for details.
- Move data fetching code or side effects to componentDidUpdate.
- If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at:
- Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.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.Please update the following components: ReactSwipableView
is there any way to get rid of this error i did try UNSAFE_componentWillReceiveProps but nothing change
The useEffect hook is also the equivalent of the componentWillReceiveProps or componentDidUpdate hooks. All we have to do is to pass in an array with the value that we want to watch for changes.
getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing. This method exists for rare use cases where the state depends on changes in props over time.
React 17 deletes the optimization of “event pooling” from React. In major browsers, it does not improve efficiency and confuses even experienced react users. React reused the event objects for output in older browsers between separate events and set all event fields to null.
It seems this has been reported to the maintainers already.
Now, as a consumer of an open source software, you may:
componentWillReceiveProps
in the repo
Ultimately, this isn't an error related to your software, but the dependencies it relies on. It isn't really your responsibility to fix those. If your app runs, it'll be fine. Warnings from react-dom.development.js
won't appear in production.
Use getDerivedStateFromProps
instead of componentWillReceiveProps
For example:
Before:
// Before
class ExampleComponent extends React.Component {
state = {
isScrollingDown: false,
};
componentWillReceiveProps(nextProps) {
if (this.props.currentRow !== nextProps.currentRow) {
this.setState({
isScrollingDown:
nextProps.currentRow > this.props.currentRow,
});
}
}
}
After:
// After
class ExampleComponent extends React.Component {
// Initialize state in constructor,
// Or with a property initializer.
state = {
isScrollingDown: false,
lastRow: null,
};
static getDerivedStateFromProps(props, state) {
if (props.currentRow !== state.lastRow) {
return {
isScrollingDown: props.currentRow > state.lastRow,
lastRow: props.currentRow,
};
}
// Return null to indicate no change to state.
return null;
}
}
https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html
I had issues finding where in my code componentWillReceiveProps was being called. I did notice in the warning it mentioned a particular component, "Drawer" which was part of the ant-d lib we are using. After upgrading ant-d to the latest version, the warning went away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With