dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Document the difference between `use_context` and `consume_context`

Open mamekoro opened this issue 11 months ago • 2 comments

I'm currently learning Dioxus, but I often feel that the documentation regarding the differences in similar features is insufficient.

I've read the following pages, but they don't explain the difference between use_context and consume_context.

  • https://dioxuslabs.com/learn/0.6/guide/state#global-state-with-context
  • https://docs.rs/dioxus/latest/dioxus/prelude/fn.use_context.html
  • https://docs.rs/dioxus/latest/dioxus/prelude/fn.consume_context.html

Could you document the difference between them and when to use which?

mamekoro avatar Mar 12 '25 08:03 mamekoro

The only difference is use_context is a hook and must be called with the rules of hooks. consume_context can be called from anywhere inside the dioxus runtime

ealmloff avatar Mar 12 '25 13:03 ealmloff

I agree, a short section in the state part of the guide would be nice.

For example, now knowing that use_context is a hook wrapper, why would I ever want to use use_context over consume_context? This is still not clear to me, seems to just be adding constraints for no benefit?

SwayStar123 avatar May 30 '25 17:05 SwayStar123