Improve inline docs (#2460)
Improve inline docs * improve incorrect event handler return error message * Improve event handler docs * document the eval functions * document spawn and common spawn errors * fix event handler docs * add notes about how you use attributes and elements in rsx * add doc aliases for attributes and events we rename * add some more aliases for common search terms * don't doc ignore any public examples in core * don't ignore public doc examples in ssr * don't ignore examples in the dioxus package readme * add a warning when you launch without a renderer enabled * fix some outdated element docs * add a bunch of examples to resource * add notes about desktop events * add more docs for use_resource * add on_unimplemented hint to Dependency * fix some unresolved links * add examples to each of the router traits * add not implemented errors for router traits * add an example to the routable trait * expand rsx macro docs * improve memo docs * update the dioxus readme * mention dioxus crate features in the docs * fix a bunch of doc tests * fix html doc tests * fix router doc tests * fix dioxus signals doc tests * fix dioxus ssr doc tests * fix use_future example in the hooks cheat sheet * add a javascript alias for eval * fix hook explanation values * remove unused embed-doc-image dependency
This commit is contained in:
1 parent
054351139f
commit
0127501dbf
71 files changed
+4132
-1162
No files matched your search
@@ -10,6 +10,30 @@ pub type ReadableRef<'a, T: Readable, O = <T as Readable>::Target> =
|
||||
<T::Storage as AnyStorage>::Ref<'a, O>;
|
||||
|
||||
/// A trait for states that can be read from like [`crate::Signal`], [`crate::GlobalSignal`], or [`crate::ReadOnlySignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Readable`] type.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// fn double(something_readable: &impl Readable<Target = i32>) -> i32 {
|
||||
/// something_readable.cloned() * 2
|
||||
/// }
|
||||
///
|
||||
/// static COUNT: GlobalSignal<i32> = Signal::global(|| 0);
|
||||
///
|
||||
/// fn MyComponent(count: Signal<i32>) -> Element {
|
||||
/// // Since we defined the function in terms of the readable trait, we can use it with any readable type (Signal, GlobalSignal, ReadOnlySignal, etc)
|
||||
/// let doubled = use_memo(move || double(&count));
|
||||
/// let global_count_doubled = use_memo(|| double(&COUNT));
|
||||
/// rsx! {
|
||||
/// div {
|
||||
/// "Count local: {count}"
|
||||
/// "Doubled local: {doubled}"
|
||||
/// "Count global: {COUNT}"
|
||||
/// "Doubled global: {global_count_doubled}"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub trait Readable {
|
||||
/// The target type of the reference.
|
||||
type Target: ?Sized + 'static;
|
||||
@@ -17,7 +41,30 @@ pub trait Readable {
|
||||
/// The type of the storage this readable uses.
|
||||
type Storage: AnyStorage;
|
||||
|
||||
/// Map the readable type to a new type.
|
||||
/// Map the readable type to a new type. This lets you provide a view into a readable type without needing to clone the inner value.
|
||||
///
|
||||
/// Anything that subscribes to the readable value will be rerun whenever the original value changes, even if the view does not change. If you want to memorize the view, you can use a [`crate::Memo`] instead.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// fn List(list: Signal<Vec<i32>>) -> Element {
|
||||
/// rsx! {
|
||||
/// for index in 0..list.len() {
|
||||
/// // We can use the `map` method to provide a view into the single item in the list that the child component will render
|
||||
/// Item { item: list.map(move |v| &v[index]) }
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // The child component doesn't need to know that the mapped value is coming from a list
|
||||
/// #[component]
|
||||
/// fn Item(item: MappedSignal<i32>) -> Element {
|
||||
/// rsx! {
|
||||
/// div { "Item: {item}" }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
fn map<O>(self, f: impl Fn(&Self::Target) -> &O + 'static) -> MappedSignal<O, Self::Storage>
|
||||
where
|
||||
Self: Clone + Sized + 'static,
|
||||
@@ -80,6 +127,38 @@ pub trait Readable {
|
||||
fn peek_unchecked(&self) -> ReadableRef<'static, Self>;
|
||||
|
||||
/// Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// fn MyComponent(mut count: Signal<i32>) -> Element {
|
||||
/// let mut event_source = use_signal(|| None);
|
||||
/// let doubled = use_memo(move || {
|
||||
/// // We want to log the value of the event_source, but we don't need to rerun the doubled value if the event_source changes (because the value of doubled doesn't depend on the event_source)
|
||||
/// // We can read the value with peek without subscribing to updates
|
||||
/// let click_count = count.peek();
|
||||
/// tracing::info!("Click count: {click_count:?}");
|
||||
/// count() * 2
|
||||
/// });
|
||||
/// rsx! {
|
||||
/// div { "Count: {count}" }
|
||||
/// div { "Doubled: {doubled}" }
|
||||
/// button {
|
||||
/// onclick: move |_| {
|
||||
/// event_source.set(Some("Click me button"));
|
||||
/// },
|
||||
/// "Click me"
|
||||
/// }
|
||||
/// button {
|
||||
/// onclick: move |_| {
|
||||
/// event_source.set(Some("Double me button"));
|
||||
/// count += 1;
|
||||
/// },
|
||||
/// "Double me"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
fn peek(&self) -> ReadableRef<Self> {
|
||||
Self::Storage::downcast_lifetime_ref(self.peek_unchecked())
|
||||
|
||||
Reference in new issue
Block a user