4 ms·
The last statement isn't true unless you mean "mutability" in just the DOM elements. All sorts of things can carry state and be mutable in a React application.
by bayesian_horse 4y ago
The last statement isn't true unless you mean "mutability" in just the DOM elements.
All sorts of things can carry state and be mutable in a React application. Even the properties of a component are actually mutable, and it sometimes happens that people pass around arrays or objects and mutate them when they shouldn't.
- phailhaus 4y agoWell, javascript is mutable so technically nothing is immutable. But I'm referring to working with React's hooks, where all props and state should be treated as immutable except for refs.
- bayesian_horse 4y agoRefs are just as immutable in that perspective. And no, you're not supposed to mutate ref.current.anything outside useEffect or a callback or similar...
- phailhaus 4y agoOf course you can! That's the whole point of refs, and why you can't use them as a dependency to callbacks or effects. They're totally mutable, which makes them very tricky to deal with. I mean, I wouldn't recommend updating them outside of effects/callbacks, but it's valid. For example: you can make a ref to count the number of times React calls your render loop by updating it in the body.