mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-24 19:37:14 +00:00
commit
3a467ec745
2 changed files with 21 additions and 20 deletions
|
@ -60,9 +60,9 @@ The best use case of `use_ref` is to manage "complex" data local to your compone
|
||||||
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let val = use_state(&cx, || vec![1, 3, 3, 7]);
|
let val = use_ref(&cx, || vec![1, 3, 3, 7]);
|
||||||
let val = use_state(&cx, || (0..10000).collect::<Vec<_>x>());
|
let val = use_ref(&cx, || (0..10000).collect::<Vec<_>x>());
|
||||||
let val = use_state(&cx, || Configuration {
|
let val = use_ref(&cx, || Configuration {
|
||||||
a: "asdasd",
|
a: "asdasd",
|
||||||
// .. more complex fields
|
// .. more complex fields
|
||||||
});
|
});
|
||||||
|
@ -82,6 +82,7 @@ fn app(cx: Scope) -> Element {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline_props]
|
||||||
fn Child(cx: Scope, val: UseRef<i32>) -> Element {
|
fn Child(cx: Scope, val: UseRef<i32>) -> Element {
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,9 +92,9 @@ impl<T: 'static> UseState<T> {
|
||||||
///
|
///
|
||||||
/// ```rust, ignore
|
/// ```rust, ignore
|
||||||
/// fn component(cx: Scope) -> Element {
|
/// fn component(cx: Scope) -> Element {
|
||||||
/// let (count, set_count) = use_state(&cx, || 0);
|
/// let count = use_state(&cx, || 0);
|
||||||
/// cx.spawn({
|
/// cx.spawn({
|
||||||
/// let set_count = set_count.to_owned();
|
/// let set_count = count.to_owned();
|
||||||
/// async move {
|
/// async move {
|
||||||
/// let current = set_count.current();
|
/// let current = set_count.current();
|
||||||
/// }
|
/// }
|
||||||
|
@ -119,11 +119,11 @@ impl<T: 'static> UseState<T> {
|
||||||
///
|
///
|
||||||
/// ```rust, ignore
|
/// ```rust, ignore
|
||||||
/// fn component(cx: Scope) -> Element {
|
/// fn component(cx: Scope) -> Element {
|
||||||
/// let (value, set_value) = use_state(&cx, || 0);
|
/// let value = use_state(&cx, || 0);
|
||||||
///
|
///
|
||||||
/// rsx!{
|
/// rsx!{
|
||||||
/// Component {
|
/// Component {
|
||||||
/// handler: set_val.setter()
|
/// handler: value.setter()
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
|
@ -144,16 +144,16 @@ impl<T: 'static> UseState<T> {
|
||||||
/// # use dioxus_core::prelude::*;
|
/// # use dioxus_core::prelude::*;
|
||||||
/// # use dioxus_hooks::*;
|
/// # use dioxus_hooks::*;
|
||||||
/// fn component(cx: Scope) -> Element {
|
/// fn component(cx: Scope) -> Element {
|
||||||
/// let (value, set_value) = use_state(&cx, || 0);
|
/// let value = use_state(&cx, || 0);
|
||||||
///
|
///
|
||||||
/// // to increment the value
|
/// // to increment the value
|
||||||
/// set_value.modify(|v| v + 1);
|
/// value.modify(|v| v + 1);
|
||||||
///
|
///
|
||||||
/// // usage in async
|
/// // usage in async
|
||||||
/// cx.spawn({
|
/// cx.spawn({
|
||||||
/// let set_value = set_value.to_owned();
|
/// let value = value.to_owned();
|
||||||
/// async move {
|
/// async move {
|
||||||
/// set_value.modify(|v| v + 1);
|
/// value.modify(|v| v + 1);
|
||||||
/// }
|
/// }
|
||||||
/// });
|
/// });
|
||||||
///
|
///
|
||||||
|
@ -185,9 +185,9 @@ impl<T: 'static> UseState<T> {
|
||||||
/// # use dioxus_core::prelude::*;
|
/// # use dioxus_core::prelude::*;
|
||||||
/// # use dioxus_hooks::*;
|
/// # use dioxus_hooks::*;
|
||||||
/// fn component(cx: Scope) -> Element {
|
/// fn component(cx: Scope) -> Element {
|
||||||
/// let (value, set_value) = use_state(&cx, || 0);
|
/// let value = use_state(&cx, || 0);
|
||||||
///
|
///
|
||||||
/// let as_rc = set_value.get();
|
/// let as_rc = value.get();
|
||||||
/// assert_eq!(as_rc.as_ref(), &0);
|
/// assert_eq!(as_rc.as_ref(), &0);
|
||||||
///
|
///
|
||||||
/// # todo!()
|
/// # todo!()
|
||||||
|
@ -207,12 +207,12 @@ impl<T: 'static> UseState<T> {
|
||||||
///
|
///
|
||||||
/// ```rust, ignore
|
/// ```rust, ignore
|
||||||
/// fn component(cx: Scope) -> Element {
|
/// fn component(cx: Scope) -> Element {
|
||||||
/// let (count, set_count) = use_state(&cx, || 0);
|
/// let count = use_state(&cx, || 0);
|
||||||
/// cx.spawn({
|
/// cx.spawn({
|
||||||
/// let set_count = set_count.to_owned();
|
/// let count = count.to_owned();
|
||||||
/// async move {
|
/// async move {
|
||||||
/// // for the component to re-render
|
/// // for the component to re-render
|
||||||
/// set_count.needs_update();
|
/// count.needs_update();
|
||||||
/// }
|
/// }
|
||||||
/// })
|
/// })
|
||||||
/// }
|
/// }
|
||||||
|
@ -237,9 +237,9 @@ impl<T: Clone> UseState<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let (val, set_val) = use_state(&cx, || 0);
|
/// let val = use_state(&cx, || 0);
|
||||||
///
|
///
|
||||||
/// set_val.with_mut(|v| *v = 1);
|
/// val.with_mut(|v| *v = 1);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn with_mut(&self, apply: impl FnOnce(&mut T)) {
|
pub fn with_mut(&self, apply: impl FnOnce(&mut T)) {
|
||||||
let mut slot = self.slot.borrow_mut();
|
let mut slot = self.slot.borrow_mut();
|
||||||
|
@ -269,9 +269,9 @@ impl<T: Clone> UseState<T> {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// let (val, set_val) = use_state(&cx, || 0);
|
/// let val = use_state(&cx, || 0);
|
||||||
///
|
///
|
||||||
/// *set_val.make_mut() += 1;
|
/// *val.make_mut() += 1;
|
||||||
/// ```
|
/// ```
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn make_mut(&self) -> RefMut<T> {
|
pub fn make_mut(&self) -> RefMut<T> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue