I am trying to create 4 variables (ref
)
Here is my way to do it;
const ref1 = useRef()
const ref2 = useRef()
const ref3 = useRef()
const ref4 = useRef()
However, in some other components, the number of ref
is going up.
So, I want to create a more dynamic way to create ref
and have it return;ref1,ref2,ref3,ref4...
My brief idea is to use Array.from()
with a specified length and a callback function that returns useRef()
[ref1, ref2, ref3, ref4, ref5] = Array.from({length: 5}, (_, i) => useRef())
However I can’t figure out how to link up the i
so that I have a array of ref<i>
[ref1, ref2, ref3, ref4, ref5] = Array.from({length: 5}, (_, i) => useRef())
3
Answers
You can set multiple variables to the same value by:
but I don’t think it would work in this case as each useRef needs to be a new one.
In an array you could:
if you want to use an index you can:
and you can put any function in the map
e.g.
as other users have said it may be better just to store all these refs in an array.
This would create an array which 4 refs each initially being 0, 1, 2, 3 respectively.
Several comments mentioned the rules of hooks but didn’t elaborate on the significance.
Much of the internal implementation of hooks requires that a component references exactly the same hooks, exactly the same number, in exactly the same order. Think of it as the price of entry. That’s why the rules specify that hooks can’t appear in conditionals or loops.
It’s not clear what you’re trying to do by having some indeterminate number of refs in the same component. Maybe you can achieve it in a more Reacty way by introducing a subcomponent, with one ref in each subcomponent.
If though you’re just looking for a way to store a collection of persisted values, perhaps instead of creating an array of refs, you could create a ref that contains an array. You could even make the array contain "ref-like" objects suitable for passing as component
ref
properties:A similar approach also works if you really really want to keep the variable names for some unspecified reason:
It’s difficult to advise further without knowing what you’re hoping to achieve with this array of refs, but in any case breaking the rules of hooks isn’t the way to go about it.
You cannot use hooks conditionally. That includes using them in
if
statements, loops, or inside of callbacks. Hooks can be composed of other hooks, but alluse*
calls must be declared at the top-level of your component. For more info, see Rules of Hooks in the React guide –✅ Call them at the top level in the body of a function component.
✅ Call them at the top level in the body of a custom Hook.
🔴 Do not call Hooks inside conditions or loops.
🔴 Do not call Hooks after a conditional return statement.
🔴 Do not call Hooks in event handlers.
🔴 Do not call Hooks in class components.
🔴 Do not call Hooks inside functions passed to
useMemo
,useReducer
, oruseEffect
.So you want an array of multiple refs? Simple, Use a single ref to an array! We can use
onChange
for each<input>
to set the corresponding index in our array ref. Run the demo below to verify in your own browser –If you want an actual array of refs, that is still possible by creating your own ref-compatible objects,
{ current: … }
. In the demo below, you can see how our refs can be used in theref
attribute of the inputs. Instead of responding toonChange
, theonSubmit
callback extracts the current value from each<input>
. Run the demo below to verify in your own browser –