Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit e738cd6

Browse files
committed
Replace /useState with useInstance hook
1 parent f97f121 commit e738cd6

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/Tippy.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ssrSafeCreateDiv,
1414
preserveRef,
1515
updateClassName,
16+
useInstance,
1617
} from './utils'
1718

1819
// React currently throws a warning when using useLayoutEffect on the server. To
@@ -43,27 +44,26 @@ function Tippy({
4344
const isControlledMode = visible !== undefined
4445

4546
const [mounted, setMounted] = useState(false)
46-
// useImperativeInstance
47-
const $this = useState({
47+
const component = useInstance(() => ({
4848
container: ssrSafeCreateDiv(),
4949
renders: 1,
50-
})[0]
50+
}))
5151

5252
const options = {
5353
ignoreAttributes,
5454
multiple,
5555
...restOfNativeProps,
56-
content: $this.container,
56+
content: component.container,
5757
}
5858

5959
if (isControlledMode) {
6060
options.trigger = 'manual'
6161
}
6262

6363
useIsomorphicLayoutEffect(() => {
64-
const instance = tippy($this.ref, options)
64+
const instance = tippy(component.ref, options)
6565

66-
$this.instance = instance
66+
component.instance = instance
6767

6868
if (onCreate) {
6969
onCreate(instance)
@@ -86,12 +86,12 @@ function Tippy({
8686

8787
useIsomorphicLayoutEffect(() => {
8888
// Prevent this effect from running on 1st render
89-
if ($this.renders === 1) {
90-
$this.renders++
89+
if (component.renders === 1) {
90+
component.renders++
9191
return
9292
}
9393

94-
const { instance } = $this
94+
const { instance } = component
9595

9696
instance.set(options)
9797

@@ -112,7 +112,7 @@ function Tippy({
112112

113113
useIsomorphicLayoutEffect(() => {
114114
if (className) {
115-
const { tooltip } = $this.instance.popperChildren
115+
const { tooltip } = component.instance.popperChildren
116116
updateClassName(tooltip, 'add', className)
117117
return () => {
118118
updateClassName(tooltip, 'remove', className)
@@ -124,11 +124,11 @@ function Tippy({
124124
<>
125125
{cloneElement(children, {
126126
ref(node) {
127-
$this.ref = node
127+
component.ref = node
128128
preserveRef(children.ref, node)
129129
},
130130
})}
131-
{mounted && createPortal(content, $this.container)}
131+
{mounted && createPortal(content, component.container)}
132132
</>
133133
)
134134
}

src/TippyGroup.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { Children, cloneElement, useState, useEffect } from 'react'
1+
import { Children, cloneElement, useEffect } from 'react'
22
import PropTypes from 'prop-types'
33
import tippy from 'tippy.js'
4+
import { useInstance } from './utils'
45

56
export default function TippyGroup({ children, ...props }) {
6-
// useImperativeInstance
7-
const $this = useState({ instances: [] })[0]
7+
const component = useInstance({ instances: [] })
88

99
useEffect(() => {
10-
tippy.group($this.instances, props)
10+
tippy.group(component.instances, props)
1111
})
1212

1313
return Children.map(children, child => {
@@ -17,7 +17,7 @@ export default function TippyGroup({ children, ...props }) {
1717
child.props.onCreate(instance)
1818
}
1919

20-
$this.instances.push(instance)
20+
component.instances.push(instance)
2121
},
2222
})
2323
})

src/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useRef } from 'react'
2+
13
export const isBrowser =
24
typeof window !== 'undefined' && typeof document !== 'undefined'
35

@@ -23,3 +25,16 @@ export function updateClassName(tooltip, action, classNames) {
2325
}
2426
})
2527
}
28+
29+
export function useInstance(initialValue = {}) {
30+
// Using refs instead of state as it's recommended to not store imperative
31+
// values in state due to memory problems in React(?)
32+
const ref = useRef()
33+
34+
if (!ref.current) {
35+
ref.current =
36+
typeof initialValue === 'function' ? initialValue() : initialValue
37+
}
38+
39+
return ref.current
40+
}

0 commit comments

Comments
 (0)