Best practice for getting ledger state in multiple nested react components
- bernhardOP 1
Suppose I have a tempalte
Fooand to react componentsOuterandInner, whereInneris a child ofOuter. If I needuseStreamQueries(Foo)in bothOuterandInner, is there an advantage to passing the result as a prop fromOutertoInner, or is it just as good to calluseStreamQueriesa second time inInner?I’m mostly interested in whether the second option will generate double the traffic over websockets.
- cocreature 2
The second option will generate 2 websockets connections and at the moment also 2 transaction streams to the legder so I’d definitely recommend passing the data down instead of querying the same data multiple times.
- bernhard 1
How would I do that in typescript? The query result has type
QueryResult<Foo, Foo.Key, "FoosTemplateId">. How do I get my hands on “FoosTempalteId” to declare the type of the props ofInner? - cocreature 2
Here’s an example. We should add a helper
User.User.QueryResultsimilarly to how we haveUser.User.CreateEvent. While the solution here works it looks a bit awkward:const allUser: QueryResult<User.User, User.User.Key, typeof User.User.templateId> = useStreamQueries(User.User); const allUserContracts: readonly User.User.CreateEvent[] = useStreamQueries(User.User).contracts; - bernhard 1
Ahh, it was the
typeofmagic I was missing, thanks. The helper would indeed be nice.