-
-
Notifications
You must be signed in to change notification settings - Fork 18
[feat] no-direct-set-state-in-use-effect: option to only warn on symmetric set function call with dependency #745
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
Comments
This is exactly the bad pattern this eslint rule is trying to report. Always use |
Hmm, useMemo isn't commonly used to retrieve data from an external location eg. data fetching, so I would still say there's a valid (if maybe not always best practice for performance) use case. Fetching in useEffect is a simple, widespread pattern. It can be replaced by RSC and data fetching libraries and can have some downsides, but it's not always wrong. Gut feel says there are also other use cases other than data fetching where using a I still think the option suggestion is valid. |
Then you will typically call If you are calling the |
Oh nice, that's great! That's one step closer to making it possible for us to adopt (at least for our projects - for students and learning, it's probably still too strict) What about other patterns to call const { globalData, setGlobalData } = useContext(globalDataContext)
useEffect(() => {
setGlobalData(componentLocalData)
}, []) Maybe in that case, you would recommend setting state during render? -useEffect(() => {
+if (/* check if componentLocalData is already in globalData */) {
setGlobalData(componentLocalData)
-}, [])
+} Feels weird to do this, but I guess as long as you have a good |
Would you like to give me an example of "component-local" information? See also: https://react.dev/learn/thinking-in-react#step-4-identify-where-your-state-should-live |
I think there are still use cases where being less strict with this rule would be nice (could also apply to teams which prefer rules to be less strict). For these types of use cases, would the maintainers here consider an option (default |
If it is for students, then IMHO this rule is a must-have. It is teaching them not to abuse |
I agree that teaching about avoiding extra state variables for derived state is good! (we do this already, in multiple ways, lots of demonstrations in lectures and showcasing Kent C. Dodds' post about deriving state) However, this rule is too general and strict for this - banning all
But this is not only about the students / teaching case - I am proposing this also as a less strict alternative of this rule, which we (and I'm guessing, other teams) could adopt right away without thinking too much about it. And in future, incrementally changing our configuration to be more strict, if we would like to further improve. |
Hi there! import { useEffect, useState } from "react";
export function List() {
const [items, setItems] = useState([4, 3, 2, 1, 0]);
const [limit, setLimit] = useState(false);
useEffect(() => {
setItems(x => [...x].reverse());
}, [limit]);
// ...Many other hooks between the two `useEffect` calls
useEffect(() => {
setLimit(x => !x);
}, [items]);
// ...
return null;
} We probably need a new specialized rule to prevent update loops in render cascades. FYI: |
Good point! I was thinking of just targeting the simple cases at first for an MVP, but it does make sense to make it more robust for loops across multiple |
Should this issue be closed now in favor of #755 ? |
Describe the problem
Hi @Rel1cx, thanks for this plugin, amazing!
I'm looking to ban the usage of symmetrical
set
functions inuseEffect
without banningset
functions entirely:Only
setLimit
would be a reported problem (because of thelimit
in the dependency array), notsetItems
Describe the solution you'd like
A new option (default
true
? would be a breaking change though) to only check for symmetrical settersMaybe this also affects
no-direct-symmetrical-set-state-in-use-layout-effect
?Alternatives considered
A new rule
no-direct-symmetrical-set-state-in-use-effect
(maybe alsono-direct-symmetrical-set-state-in-use-layout-effect
?)Additional context
This would be a less strict rule that would catch a lot of infinite loop patterns without restricting
set
functions for use cases that can make senseUse case: teaching students to avoid infinite loops while not being overly strict:
The text was updated successfully, but these errors were encountered: