3 ms·
For one thing you may not use the ref attribute on function components because they don’t have instances. That means the component can't have a .focus() method
by flowerlad 6y ago
For one thing you may not use the ref attribute on function components because they don’t have instances. That means the component can't have a .focus() method for example.
For simple components hooks may be fine. For more complex ones I prefer classes.
- Octoth0rpe 6y agoI believe the useRef hook is used for this problem: https://reactjs.org/docs/hooks-reference.html#useref https://reactjs.org/docs/hooks-reference.html#useref The example even uses .focus()
- cnity 6y agoThat's just not true: https://reactjs.org/docs/forwarding-refs.html https://reactjs.org/docs/forwarding-refs.html
- avery42 6y agoMaybe I misunderstood, but isn't this what `React.forwardRef` is for?
- evan_ 6y agoalong with useImperativeHandle for writing your own imperative functions like focus() or whatever