Inline ref functions in React

Just a guy who loves to write code and watch anime.
Search for a command to run...

Just a guy who loves to write code and watch anime.
No comments yet. Be the first to comment.
Qualities that make outstanding builders.

Bipedal vs quadrupedal: how many legs This is about the number of legs the creature walks on. Bipedal. Two legs. Humans, ostriches, T-rex, kangaroos, most fantasy humanoids. Quadrupedal. Four legs. Do

Intro You hear "lerp" and "smoothstep" everywhere in game dev. They sound like math jargon. They're not. Both are small tools that do the same job: smoothly move from one value to another. The problem

What is Kinematics Kinematics is the math field. It's the study of how things move without worrying about forces (which would be dynamics). FK and IK are the two branches: forward kinematics and inver

Intro Textures are usually the biggest cost in a 3D scene. Memory, bandwidth, and load time all get eaten by them. Resizing them is the obvious lever. There's more. This post is about the less obvious

In React, you can use inline functions to get element references (refs), but this approach has some nuances and is generally not recommended due to performance issues.
I tried it in one of my projects and realized that the function kept running on every render. Which led to this blog post haha.
Inline ref functions are defined directly within the JSX of a component. When you use an inline function for a ref, React calls this function twice during renders: first with null to clear the previous ref, and then with the new DOM element. This happens because a new instance of the function is created with each render, so React needs to clear the old ref and set up the new one.
function MyComponent() {
let inputRef;
return (
<div>
<input
ref={(el) => {
inputRef = el;
}}
/>
<button onClick={() => console.log(inputRef)}>Log Input Ref</button>
</div>
);
}
In this example, the inline function (el) => { inputRef = el; } is called twice on each render: once with null and once with the actual DOM element.
useRefs aren't called twice on each render. They only set the ref once, even if re-renders happen, they're stable.
useRefs are more performant.
As mentioned, they should be used sparingly, but can be useful when you need:
Dynamic refs.
Conditional refs.
function DynamicRefsComponent({ items }) {
const refs = {};
return (
<div>
{items.map((item, index) => (
<div
key={index}
ref={(el) => { refs[item.id] = el; }}
>
{item.content}
</div>
))}
<button onClick={() => console.log(refs)}>Log All Refs</button>
</div>
);
}
Here we store the refs in a refs object. This is dynamically created by the ref prop. We associate each ref with the id of the item.
Mind you, you're rarely gonna do this when working with React. I've never needed to do this. I literally had to look up when inline ref functions are useful because I couldn't think of a situation myself.
Inline ref functions should be used sparingly.