We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc6a764 commit a623d5dCopy full SHA for a623d5d
1 file changed
src/ref-component.tsx
@@ -4,15 +4,18 @@ type RefUpdate = (ref: HTMLElement) => void
4
type BaseProps = { init: RefUpdate, unmount: RefUpdate } & Record<string, unknown>
5
6
export function RefComponent<Props extends BaseProps>({ init, unmount, ...props }: Props) {
7
- const r = React.useRef<HTMLSpanElement | null>()
+ const ref = React.useRef<HTMLSpanElement>(null)
8
9
- return <span {...props} ref={ref => {
10
- if (r.current) {
11
- unmount(r.current)
12
- }
13
- if (ref) {
14
- r.current = ref
15
- init(ref)
+ React.useEffect(() => {
+ const element = ref.current
+
+ return () => {
+ if (element) unmount(element)
16
}
17
- }}/>
+ }, [])
+ if (ref.current) init(ref.current)
18
+ })
19
20
+ return <span {...props} ref={ref} />
21
0 commit comments