Skip to content

The display is inconsistent due to the location of provide_context. #2038

@lizidev

Description

@lizidev

Describe the bug

The display is inconsistent due to the location of provide_context.

Leptos Dependencies

Please copy and paste the Leptos dependencies and features from your Cargo.toml.

For example:

leptos = { version = "0.5.2", features = ["csr"] }

Modified at CodeSandbox at https://leptos-rs.github.io/leptos/view/08_parent_child.html

use leptos::*;

#[derive(Copy, Clone)]
struct SmallcapsContext(WriteSignal<bool>);

#[component]
pub fn App() -> impl IntoView {
    let (red, set_red) = create_signal(false);
    provide_context(SmallcapsContext(set_red));

    view! {
        <main>
            // here!!!
            <div>
                <p class:red=red>"apple"</p>
                <Button/>
            </div>
            <App2/>
        </main>
    }
}

#[component]
pub fn Button() -> impl IntoView {
    let setter = use_context::<SmallcapsContext>().unwrap().0;

    view! {
        <button on:click=move |_| {
            setter.update(|value| *value = !*value)
        }>"Toggle Red"</button>
    }
}

#[component]
fn App2() -> impl IntoView {
    let (red, set_red) = create_signal(false);
    provide_context(SmallcapsContext(set_red));

    view! {
        <main>
            <p class:red=red>
                "apple2"
            </p>
            <Button2/>
        </main>
    }
}

#[component]
pub fn Button2() -> impl IntoView {
    let setter = use_context::<SmallcapsContext>().unwrap().0;
    
    view! {
        <button on:click=move |_| {
            setter.update(|value| *value = !*value)
        }>"2. Toggle Red"</button>
    }
}

fn main() {
    leptos::mount_to_body(App)
}

To Reproduce
Steps to reproduce the behavior:

Change the position of div and App2 under the main tag.

  1. Click the Button Button The apple text turns red. Click the Button2 Button The apple2 text turns red.
<div>
  <p class:red=red>"Lorem ipsum sit dolor amet."</p>
  <Button/>
</div>
<App2/>
  1. Click the Button Button The apple2 text turns red. Click the Button2 Button The apple2 text turns red.
<App2/>
<div>
  <p class:red=red>"Lorem ipsum sit dolor amet."</p>
  <Button/>
</div>

Expected behavior

Regardless of location, you should:

Click the Button Button The apple text turns red. Click the Button2 Button The apple2 text turns red.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions