T
TanStack12mo ago
plain-purple

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
conscious-sapphire
conscious-sapphire12mo 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...
conscious-sapphire
conscious-sapphire12mo ago
you can call .navigate on the router object returned by createRouter
plain-purple
plain-purpleOP12mo ago
Ok but how would you recommend storing the router object?
conscious-sapphire
conscious-sapphire12mo 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
plain-purple
plain-purpleOP12mo ago
Hmm I see! Thanks, I'll try that!

Did you find this page helpful?