DioxusLabs / DioxusLabs/dioxus

`try_use_context` Is Not Reactive When `None`

Open
#4,509 6 comments 0 reactions 0 assignees View on GitHub
core
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

If you use `try_use_context` in a descendant and receive `None`, and then later provide the context through `provide_context` in an ancestor, the descendant does not react to this.

**Steps To Reproduce**

```rust
use dioxus::{logger::tracing::Level, prelude::*};

fn main() {
dioxus::logger::init(Level::DEBUG).expect("failed to init logger");
dioxus::launch(App);
}

#[component]
fn App() -> Element {
rsx! {
Descendent {}
button {
onclick: move |_| {
provide_context(String::new());
info!("Provided context");
},
"Click me to provide context"
}
}
}

#[component]
fn Descendent() -> Element {
let x = try_use_context::();
rsx! {
if x.is_some() {
"Is Some"
} else {
"Is None"
}
}
}
```

**Expected behavior**

`try_use_context` should be reactive in the `None` case.

**Current Alternative**

Use `provide_context` context with `None` at the root of the application. e.g.

`provide_context::>(Signal::new(None))`

Then use in a descendant with a regular `use_context` (no need for `try_use_context`).

**Environment:**

- Dioxus version: 0.7.0-alpha.3

**Questionnaire**

I'm interested in fixing this myself but don't know where to start.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.