Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nonreactive examples for hooks were not compilable as written #2337

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rogusdev
Copy link
Sponsor Contributor

@rogusdev rogusdev commented Apr 18, 2024

My code was out of date, so I missed b6d3da2 -- but I still caught one more, and added an example. Also tweaked the comments.

@rogusdev rogusdev force-pushed the correct-nonreactive-examples branch from 59e683d to 4b096ee Compare April 18, 2024 16:53
@rogusdev
Copy link
Sponsor Contributor Author

Notably: for the example, I did not call &*res.read_unchecked() as the docs suggest on https://dioxuslabs.com/learn/0.5/reference/use_resource -- instead choosing to simply do res(), which definitely works. If the longer syntax is preferred / recommended, I would like to update my example to keep best practices in all places, but I'd also like to hear why, thanks.

}

#[component]
fn Blog(id: i32) -> Element {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really good use case for ReadOnlySignal<i32>. We automatically convert T to ReadOnlySignal<T> in the props so it will work out of the box with the router and it will be reactive without the extra use_reactive hook

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_reactive is good for non-reactive state that is created in the middle of a component like this:

let state = use_signal(|| 0);
let doubled = state() * 2;
use_effect(use_reactive((&doubled,), |(doubled,)| println!("Manually manipulate the dom")));

Copy link
Sponsor Contributor Author

@rogusdev rogusdev Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense, however 2 questions then:

  1. Is that distinction documented somewhere? After this PR gets merged (in some form), I already intend to update https://dioxuslabs.com/learn/0.5/reference/use_resource with a link to the example and an explanation
  2. If it is already done with props, so should it happen with the #[component] approach as well? As in, should this just work without the use_reactive or explicit ReadOnlySignal at all? -- Oh wait, or do you mean if I mark ReadOnlySignal<T> in the component args, I just don't have to wrap it with ReadOnlySignal when passing in?

Copy link
Member

@ealmloff ealmloff Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is that distinction documented somewhere? After this PR gets merged (in some form), I already intend to update https://dioxuslabs.com/learn/0.5/reference/use_resource with a link to the example and an explanation

Not yet 🙂

  1. If it is already done with props, so should it happen with the #[component] approach as well? As in, should this just work without the use_reactive or explicit ReadOnlySignal at all?

The conversion happens when you use the component if you explicitly use ReadOnlySignal<T> in your props or #[component].

// Because value is a signal, it is reactive and any reads will automatically add the signal as a dependency to use_effect, use_resource, etc
#[component]
fn Comp(value: ReadOnlySignal<i32>) -> Element {None}

Comp {
   // A new signal is created here
   value: 0
}

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update the example, and also add documentation around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants