@shopify/shop-minis-react
Version:
React component library for Shopify Shop Minis with Tailwind CSS v4 support (source-only, requires TypeScript)
17 lines (13 loc) • 523 B
text/typescript
import {useEffect, useRef} from 'react'
export function useOnNavigateBack(callback: () => void) {
// Using a ref allows the callback to be updated without triggering a re-render
// This makes the hook nicer to use because developers don't need useCallback
const callbackRef = useRef(callback)
callbackRef.current = callback
useEffect(() => {
const listenerId = window.minisEvents.on('NAVIGATE_BACK', () => {
callbackRef.current()
})
return () => window.minisEvents.off(listenerId)
}, [])
}