How to tell the compiler that something is a ref and can be mutated? #36
Answered
by
aeharding
gaearon
asked this question in
General Questions
|
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? |
Answered by
aeharding
Nov 17, 2024
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 |
1 reply
|
enableTreatRefLikeIdentifiersAsRefs should work: |
5 replies
Answer selected by
gaearon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

enableTreatRefLikeIdentifiersAsRefs should work:
https://playground.react.dev/#N4Igzg9grgTgxgUxALhAejQAgAIIHYCGARgDYIAqMCBALgEoIBmAMgJYDWCAkgCb42tGrBDDABBMA0ZgAOnjkIAHgAcIMGpkZQ8cARDyYA4gRJkYATy40EAWwAUwOZkwZMAOg9PMNABZQbRAAirDaSTAA0cgC+yJgACjAQymAAlJiOBi5YHm5eVDSwBnZezq45JZgAPFw2BADmCBXO+swQBDwAvMAImB0AfOlNzlmYCGAkrHg0sQCyUDS0k3WYcBA2qnj8mMqJyZhqmD4QEOyYBDB1-vxgmKw3eBAaJiQQAO4IPG6YAML6YKx8GCYKD-PDLAiYF5wEyYABu51YxDItzwYGs7SGzl8-iCITCjDccFgVCmAG1JnxFABdXqDTLDBmYV4A3yxBBuSCwRBuZk8XyRemM5w+BCsOo+aajDnQeDskViiUCoXOKJDKKqwVoPpeFLREBRIA