How to tell the compiler that something is a ref and can be mutated? #36
-
This is getting flagged by the rule: export function GalleryItem({
// ...
thumbDimsRef,
}: Props) {
// ...
return (
// ...
<Image
onLoad={e => {
// eslint: Mutating component props or hook arguments is not allowed. Consider using a local variable instead
thumbDimsRef.current[index] = {
width: e.source.width,
height: e.source.height,
}
}}
/> How do I explain to the compiler that this is safe? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Another question is how to model the "mutable stuff in state" pattern. React.useEffect(() => {
if (state.needsPersist) {
state.needsPersist = false
// ...
}
}, [state]) I know this isn't exactly blessed, but I recall discussions about maybe changing the inner type to a ref to communicate to the compiler that this information isn't used for rendering. However, holding a ref in state and modifying its |
Beta Was this translation helpful? Give feedback.
-
enableTreatRefLikeIdentifiersAsRefs should work: |
Beta Was this translation helpful? Give feedback.
enableTreatRefLikeIdentifiersAsRefs should work:
https://playground.react.dev/#N4Igzg9grgTgxgUxALhAejQAgAIIHYCGARgDYIAqMCBALgEoIBmAMgJYDWCAkgCb42tGrBDDABBMA0ZgAOnjkIAHgAcIMGpkZQ8cARDyYA4gRJkYATy40EAWwAUwOZkwZMAOg9PMNABZQbRAAirDaSTAA0cgC+yJgACjAQymAAlJiOBi5YHm5eVDSwBnZezq45JZgAPFw2BADmCBXO+swQBDwAvMAImB0AfOlNzlmYCGAkrHg0sQCyUDS0k3WYcBA2qnj8mMqJyZhqmD4QEOyYBDB1-vxgmKw3eBAaJiQQAO4IPG6YAML6YKx8GCYKD-PDLAiYF5wEyYABu51YxDItzwYGs7SGzl8-iCITCjDccFgVCmAG1JnxFABdXqDTLDBmYV4A3yxBBuSCwRBuZk8XyRemM5w+BCsOo+aajDnQeDskViiUCoXOKJDKKqwVoPpeFLREBRIA