Skip to content

Commit 20e2c09

Browse files
OupslaGabriel Jablonski
authored and
Gabriel Jablonski
committed
fix(callbacks): fix some edge cases by using useEffect
1 parent 3e83356 commit 20e2c09

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/App.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,30 @@ function App() {
177177
afterHide={() => console.log('After hide')}
178178
content="Showing tooltip and calling afterShow method"
179179
/>
180+
181+
<button id="buttonCallbacksClick">With click event</button>
182+
<Tooltip
183+
events={['click']}
184+
place="bottom"
185+
anchorId="buttonCallbacksClick"
186+
// eslint-disable-next-line no-console
187+
afterShow={() => console.log('After show with click')}
188+
// eslint-disable-next-line no-console
189+
afterHide={() => console.log('After hide with click')}
190+
content="Showing tooltip and calling afterShow method"
191+
/>
192+
193+
<button id="buttonCallbacksDelay">With delay</button>
194+
<Tooltip
195+
delayShow={1000}
196+
place="bottom"
197+
anchorId="buttonCallbacksDelay"
198+
// eslint-disable-next-line no-console
199+
afterShow={() => console.log('After show with delay')}
200+
// eslint-disable-next-line no-console
201+
afterHide={() => console.log('After hide with delay')}
202+
content="Showing tooltip and calling afterShow method"
203+
/>
180204
</div>
181205
</main>
182206
)

src/components/Tooltip/Tooltip.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ const Tooltip = ({
5454
} else if (isOpen === undefined) {
5555
setShow(value)
5656
}
57+
}
5758

58-
// Callbacks
59-
if (value === true && afterShow) {
60-
afterShow()
61-
}
62-
if (value === false && afterHide) {
63-
afterHide()
59+
// Callbacks
60+
useEffect(() => {
61+
if (show) {
62+
afterShow?.()
63+
} else {
64+
afterHide?.()
6465
}
65-
}
66+
}, [show])
6667

6768
const handleShowTooltipDelayed = () => {
6869
if (tooltipShowDelayTimerRef.current) {

0 commit comments

Comments
 (0)