DioxusLabs/docsite

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.

image

Above is the screenshot. Inconsistencies are circled in red:

  • Should we use use_context or consume_context? The migration guide says use_context and nothing about consume_context. And I see in the source code that use_context uses consume_context
  • The return values of use_context and consume_context are not Options, but here it says the values are Some or None

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_contextinstead, so you can handle the None 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 as use_context, you could do it yourself with use_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_contextinstead, so you can handle the None 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 as use_context, you could do it yourself with use_hook though.

Oops, should be "memoize", fixed! Thanks 😄