Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

ref-utils

v0.86.0arrow_drop_down
v0.86.0
STATUS
Passing
DOWNLOADS
4,086
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
9 months ago
SIZE
601 B
The function that correctly handles passing refs.
1 contributor
Use ref-utils in your project ?
Copied
npm i @bit/semantic-org.semantic-ui-react.utils.ref-utils
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
refUtils.js
34 Lines(21 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * The function that correctly handles passing refs.
 *
 * @param {Function|Object} ref An ref object or function
 * @param {HTMLElement} node A node that should be passed by ref
 */
export const handleRef = (ref, node) => {
  if (process.env.NODE_ENV !== 'production') {
    if (typeof ref === 'string') {
      throw new Error(
        [
          'We do not support refs as string, this is a legacy API and will be likely to be removed in',
          'one of the future releases of React.',
        ].join(' '),
      )
    }
  }

  if (typeof ref === 'function') {
    ref(node)
    return
  }

  if (ref !== null && typeof ref === 'object') {
    // The `current` property is defined as readonly, however it's a valid way because `ref` is a mutable object
    // eslint-disable-next-line no-param-reassign
    ref.current = node
  }
}

export const isRefObject = ref =>
  // https://github.com/facebook/react/blob/v16.8.2/packages/react-reconciler/src/ReactFiberCommitWork.js#L665
  // eslint-disable-next-line
  ref !== null && typeof ref === 'object' && ref.hasOwnProperty('current')