Migrating to v4
In v3 there was no React package - the docs handed you a useForesight hook to copy into your own project. v4 replaces that with the official @foresightjs/react package. Install it, delete your copied file, and update a couple of names.
Install the package and delete your hand-rolled useForesight.ts:
npm install @foresightjs/react
Then update the import and the return shape:
- import useForesight from "./hooks/useForesight"
+ import { useForesight } from "@foresightjs/react"
- const { elementRef, registerResults } = useForesight({ callback, name })
+ const { elementRef, isRegistered, isPredicted } = useForesight({ callback, name })
What changed:
- The state is flattened onto the return value. There's no
registerResultsobject anymore.registerResults.isRegisteredbecomesisRegistered, and you also get the rest of the reactive state (isPredicted,isActive,hitCount, …) which re-renders when it changes. See useForesight. elementRefis now a callback ref. You still attach it the same way (ref={elementRef}), so your JSX doesn't change.registerResults.isTouchDeviceis gone. Touch devices are handled internally by the configuredtouchDeviceStrategy, so the manual fallback branch is no longer needed.
Core changes
The core js.foresight library also has a few breaking changes. The package handles most of these for you, but they matter if you call register() or listen to events directly.
The register() call itself is unchanged, but element data is now an immutable state snapshot you can subscribe to - the foundation the wrapper packages are built on:
ForesightElementDatais nowForesightElementState. Your callback receives the flat state snapshot:callback: (state: ForesightElementState) => void. See TypeScript.register()returns the state plusunregister,subscribeandgetSnapshot.isTouchDeviceis gone from the result; touch devices are handled internally by the configuredtouchDeviceStrategyglobal setting.- The
elementDataUpdated,elementOptionsUpdatedandelementReactivatedevents are removed. Per-element changes are now observed through thesubscribe/getSnapshotpair returned byregister(). The remaining events carry the state in astatefield instead ofelementData. - Detached elements are parked, not unregistered. Removing an element from the DOM no longer emits
elementUnregisteredwith reasondisconnected; the element stays registered but inactive and resumes when it reconnects. - New
enabledelement option. Set it tofalseto keep an element registered but excluded from prediction.