consume_context or use_context? Doc inconsistency
ifsheldon opened this issue · 7 comments
Hi! I'm migrating my code from dioxus 0.4 to 0.5. I used shared states a lot, so I pay much attention to the new use_context*
APIs.
It seems the documentation is not consistent on how to access shared states in child components.
Above is the screenshot. Inconsistencies are circled in red:
- Should we use
use_context
orconsume_context
? The migration guide saysuse_context
and nothing aboutconsume_context
. And I see in the source code thatuse_context
usesconsume_context
- The return values of
use_context
andconsume_context
are notOption
s, but here it says the values areSome
orNone
I think it's better to use the use_context
. The use_context()
is just calling consume_context()
. Wouldn't this be meaningless if we didn't use use_context()
🤣?
#[must_use]
pub fn use_context<T: 'static + Clone>() -> T {
use_hook(|| consume_context::<T>())
}
This Doc should really be reviewed🤣
use_context
will be slightly faster if you have a large component tree because it only searches for the context once when the hook is first run
I'll try to get this improved
Hey @ifsheldon @ZephyrZhang3 what do you think about #256 ? Let me know if something else is missing or could be improved!
The modifications look great! Thanks!
Maybe some typo in docs-src/0.5/en/reference/context.md
?
mark with bold and italic(?)
If you have a component where the context might or not be provided, you might want to use
try_consume_context
instead, so you can handle theNone
case. The drawback of this method is that it will not memoeize(memorize?) the value between renders, so it won't be as as(as?) efficient asuse_context
, you could do it yourself withuse_hook
though.
Maybe some typo in
docs-src/0.5/en/reference/context.md
?mark with bold and italic(?)
If you have a component where the context might or not be provided, you might want to use
try_consume_context
instead, so you can handle theNone
case. The drawback of this method is that it will not memoeize(memorize?) the value between renders, so it won't be as as(as?) efficient asuse_context
, you could do it yourself withuse_hook
though.
Oops, should be "memoize", fixed! Thanks 😄