Skip to content

Commit

Permalink
update based on PR comments 1
Browse files Browse the repository at this point in the history
  • Loading branch information
voidpumpkin committed Oct 13, 2021
1 parent 337a018 commit c63c5df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/yew/src/functional/hooks/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ impl<T: fmt::Debug> fmt::Debug for UseStateHandle<T> {
}

impl<T> UseStateHandle<T> {
/// Updates the value
/// Replaces the value
///
/// *Always causes a rerender*
pub fn set(&self, value: T) {
(self.setter)(value)
}
/// Updates the value if it is different from previous value
pub fn set_neq(&self, value: T)
/// Replaces the value if it is different from previous value
///
/// **Only available for value types that implement PartialEq trait**
pub fn set_if_neq(&self, value: T)
where
T: PartialEq,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/tests/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn use_state_handle_set_neq_works() {
RENDER_COUNT += 1;
}
let counter = use_state(|| 0);
counter.set_neq(1);
counter.set_if_neq(1);

return html! {
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ description: "The pre-defined Hooks that Yew comes with "

`use_state` is used to manage state in a function component.
It returns a `UseState` object which `Deref`s to the current value
and provides `set` and `set_neq` methods to update the value.
and provides `set` and `set_if_neq` methods to update the value.
Note that `set_if_neq` is only available if your value implements `PartialEq` trait.

The hook takes a function as input which determines the initial state.
This value remains up-to-date on subsequent renders.
Expand Down

0 comments on commit c63c5df

Please sign in to comment.