Closed allan2 closed 11 months ago
Hi, I agree with you. I couldn't get the first example to work. use_context
always returned None
.
Also, the second example causes a performance problem as the context struct Theme
will be cloned for each child using it.
I am currently implementing a translation system with a struct that I want to store on the state, and it is just not right to clone all of my Translator
struct, with all of the loaded translations data, for each child.
Then I am wondering if Yew has the ability to support reference-based contexts, just like in example 1 (but it is not working), or should I use an external library?
When trying to learn contexts, I got stuck on the Docusaurus example. I had to reach for the Yew API docs (a.k.a. cargo docs for Yew Next) to get things working. That's when I noticed that the docs are inconsistent on this topic.
The first example is found in docs/concepts/contexts.mdx, which is hosted here on yew.rs. The second example is on the
use_context
page in the Yew Next cargo docs.Example 1:
Example 2 (snippet):
Example 1 uses
use_memo
andContextProvider<Rc<Theme>>
. Example 2 usesuse_state
andContextProvider<Theme>
.I suspect Example 1 is either incomplete or out-of-date because
theme
inNavButton
has typeOption<Theme>
, which is not mentioned. It also has aNone
value if you try to unwrap, which is where I got stuck. Example 2 works fine.It would be nice if the context docs were consistent. I am not still not sure whether
use_memo
oruse_state
is preferred or if both can be used, but an explanation would be helpful.