Skip to content

Instantly share code, notes, and snippets.

@YuCJ
YuCJ / compose-react-refs.jsx
Last active August 30, 2020 15:41
In React, we use `ref` to touch the DOM element directly. When a component takes refs passed by its parent, and it needs to touch the DOM element by itself also, we have to compose the refs into one so we can pass it to the child..
function updateElementInRef(element, ref) {
if (typeof ref === 'function') {
/*
The ref is a callback ref.
https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
*/
ref(element)
} else if (ref) {
/*
The ref is an object expected to be mutated with property `current`.