Skip to content

How to use React context API in remix? #1785

Closed Answered by sergiodxa
tiranuom asked this question in Q&A
Discussion options

You must be logged in to vote

It should work as usual

  1. Create a context object with React.createContext (do it outside route files)
  2. In some part render the context.Provider with some value
  3. In another part call useContext(context) to read the value

Also note that the Outlet component has a context prop (<Outlet context={value} />) and a pair hook useOutletContext which let you share values between a parent and a child route.

And, also note that if you want to access another route (loader) data you can use the useMatches hook to get all the routes data, and you can use useRouteData from Remix Utils which uses useMatches to get the data too.

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
2 replies
@tiranuom
Comment options

@felixmpaulus
Comment options

Answer selected by tiranuom
Comment options

You must be logged in to vote
3 replies
@Fantros
Comment options

@sayhicoelho
Comment options

@lanesky
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants