diff --git a/.changeset/silent-chefs-refuse.md b/.changeset/silent-chefs-refuse.md new file mode 100644 index 00000000..21b527d3 --- /dev/null +++ b/.changeset/silent-chefs-refuse.md @@ -0,0 +1,5 @@ +--- +"@react-three/rapier": patch +--- + +Throw useful error when `usePhysics` used outside `` (@CodyJasonBennett) diff --git a/packages/react-three-rapier/src/hooks/hooks.ts b/packages/react-three-rapier/src/hooks/hooks.ts index 5a049387..ffe2bf3b 100644 --- a/packages/react-three-rapier/src/hooks/hooks.ts +++ b/packages/react-three-rapier/src/hooks/hooks.ts @@ -29,8 +29,10 @@ const useMutableCallback = (fn: T) => { * Exposes the Rapier context, and world * @category Hooks */ -export const useRapier = () => { - return useContext(rapierContext) as RapierContext; +export const useRapier = (): RapierContext => { + const rapier = useContext(rapierContext); + if (!rapier) throw new Error('react-three-rapier: useRapier must be used within !') + return rapier; }; /**