# use-child-ref

> useChildRef is a React hook for getting or creating a child ref

Latest version **2.0.1** (published 2023-04-24) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install use-child-ref
pnpm add use-child-ref
yarn add use-child-ref
bun add use-child-ref
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2023-04-24 |
| First published | 2022-12-21 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 0.10.0 |
| Dependencies | 0 |
| Unpacked size | 18.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | ilvetrov |
| Maintainers | ilvetrov |
| Keywords | react |

## Links

- npm: https://www.npmjs.com/package/use-child-ref
- Repository: https://github.com/ilvetrov/use-child-ref
- npm.io page: https://npm.io/package/use-child-ref

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2023-04-24
- 1.0.2-0 (next) — 2022-12-24
- 2.0.0 — 2022-12-24
- 1.0.1 — 2022-12-21
- 1.0.0 — 2022-12-21

## README

<p>
  <img src="https://raw.githubusercontent.com/ilvetrov/use-child-ref/main/logo.svg" width="150" height="75" />
</p>

# useChildRef

`useChildRef` is a React hook for getting or creating a child ref.

## Usage

- If `props.children` has a ref, childRef will be that ref.

- If `props.children` does not have a ref, childRef will be the newly created ref.

- If `props.children` cannot have a ref, childRef will be `undefined`. For example, any of your custom components without a [forwardRef](https://reactjs.org/docs/forwarding-refs.html) cannot have a ref.

```tsx
import useChildRef from 'use-child-ref'

export default function IAmUsingChildRef(props: { children: JSX.Element }) {
  const [child, childRef] = useChildRef(props.children)

  return child
}
```

### Rules

- You must return `child` from the hook, not `props.children`, for the new ref to work.
  ```tsx
  const [child, childRef] = useChildRef(props.children)
  
  return child
  ```
- `props.children` must be one element.
  ```tsx
  // Good
  export function ParentComponent() {
    return (
      <IAmUsingChildRef>
        <div></div>
      </IAmUsingChildRef>
    )
  }

  // Bad
  export function ParentComponent() {
    return (
      <IAmUsingChildRef>
        <div></div>
        <div></div>
      </IAmUsingChildRef>
    )
  }
  ```
- Use [forwardRef](https://reactjs.org/docs/forwarding-refs.html) with your custom component if you want to give access to an element in the component via ref.
  ```tsx
  import { forwardRef } from 'react'

  export const WithoutRefAccess = () => {
    return <h1></h1>
  }

  export const WithRefAccess = forwardRef((props, ref) => {
    return <h1 ref={ref}></h1>
  })

  export const IAmUsingChildRef = (props: { children: JSX.Element }) => {
    const [child, childRef] = useChildRef(props.children)

    console.log('childRef: ', typeof childRef)

    return child
  }

  export const ParentComponent = () => {
    return (
      <>
        <IAmUsingChildRef>
          {/* childRef: undefined */}
          <WithoutRefAccess></WithoutRefAccess>
        </IAmUsingChildRef>

        <IAmUsingChildRef>
          {/* childRef: object */}
          <WithRefAccess></WithRefAccess>
        </IAmUsingChildRef>
      </>
    )
  }
  ```

### Example: Hide Component

Sometimes it's so necessary for legacy third-party code :)

```tsx
import useChildRef from 'use-child-ref'

export default function Hide(props: { children: JSX.Element; hide: boolean }) {
  const [child, childRef] = useChildRef<HTMLElement>(props.children)

  useLayoutEffect(() => {
    if (childRef?.current) {
      childRef.current.style.display = props.hide ? 'none' : ''
    }
  }, [props.hide])

  return child
}
```

---
_Source: https://npm.io/package/use-child-ref · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
