-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react/destructuring-assignment ignoreClassFields does not support nested objects #1947
Comments
I think that this is a bug, but also, you shouldn’t ever be using props in initial constructor state - you want getDerivedStateFromProps. Otherwise when props change, you might forget to update “beta” |
@ljharb Agreed. I'm going through existing code applying lint fixes and this situation popped up. The whole pattern in this particular file needs to be rewritten / refactored but for now I'm focusing on lint issues. |
What about uncontrolled components though? If the component accepts a "defaultValue" prop, I think it only needs to be "translated" to state once on initialization, and after that the value is managed via component's state. |
That’d be an edge case, and even then I’d say the component should rerender when it gets an updated prop. |
Whenever I'm in the case of having to use props in state definition, I avoid the warning by using the constructor. export default class Thing extends React.Component {
constructor(props) {
super(props);
this.state = {
alpha: false,
beta: props.beta,
charlie: null,
};
}
// ...
} About getDerivedStateFromProps, the following React post recommends avoiding it most of the time. One of the preferred solutions uses props in state definition (Fully uncontrolled component with a key) |
@Jmenache note that your pattern is not correct unless you also use setState in componentWillReceiveProps - and the replacement for that pair is indeed to use gDSFP. |
@ljharb I think the post recommends using a |
Certainly that's another workaround, but that seems much less performant and clean. |
@ljharb As counter intuitive as it is, the article explains that it is usually not slower, and sometimes faster.
I agree it is not pretty and I would most likely try using the other recommended solution if I have a choice, but this pattern helped me a few times dealing with legacy code. |
Using this rule:
This code:
Generates this warning:
Version information:
The text was updated successfully, but these errors were encountered: