Skip to content

Commit

Permalink
refactor(useRapier): throw when used outside Physics (#539)
Browse files Browse the repository at this point in the history
* refactor(useRapier): throw when used outside Physics

* chore: changesets

---------

Co-authored-by: Hugo Wiledal <[email protected]>
  • Loading branch information
CodyJasonBennett and wiledal authored Oct 17, 2023
1 parent 144881d commit c9e897a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-chefs-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-three/rapier": patch
---

Throw useful error when `usePhysics` used outside `<Physics />` (@CodyJasonBennett)
6 changes: 4 additions & 2 deletions packages/react-three-rapier/src/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ const useMutableCallback = <T>(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 <Physics />!')
return rapier;
};

/**
Expand Down

0 comments on commit c9e897a

Please sign in to comment.