T
TanStack9mo ago
magic-amber

Using Tanstack Router with WebSockets

Hey, I'm making a webapp (SPA) with React and WebSockets. I want to have access to my socket in any part of the application, I thought about using a singleton approach and instantiate it in the main.tsx. However, I would like to still be able to use the tanstack redirect methods. So what would be the best way to implement something like that?
5 Replies
blank-aquamarine
blank-aquamarine9mo ago
Router type | TanStack Router React Docs
The Router type is used to describe a router instance. Router properties and methods An instance of the Router has the following properties and methods: .update method Type: (newOptions: RouterOptions...
blank-aquamarine
blank-aquamarine9mo ago
you can call .navigate on the router object returned by createRouter
magic-amber
magic-amberOP9mo ago
Ok but how would you recommend storing the router object?
blank-aquamarine
blank-aquamarine9mo ago
storing it? you can instantiate it in a different file, and import it both where you give it to the router provider and in the file with the socket logic
magic-amber
magic-amberOP9mo ago
Hmm I see! Thanks, I'll try that!

Did you find this page helpful?