-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Introduce a dedicated use_force_update hook #2586
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use super::{Hook, HookContext}; | ||
use crate::functional::ReRender; | ||
use std::fmt; | ||
|
||
/// A handle which can be used to force a re-render of the associated | ||
/// function component. | ||
#[derive(Clone)] | ||
pub struct UseForceUpdate { | ||
trigger: ReRender, | ||
} | ||
|
||
impl fmt::Debug for UseForceUpdate { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("UseForceUpdate").finish() | ||
} | ||
} | ||
|
||
impl UseForceUpdate { | ||
/// Trigger an unconditional re-render of the associated function component | ||
pub fn force_update(&self) { | ||
(self.trigger)() | ||
} | ||
} | ||
|
||
// #![feature(fn_traits)] // required nightly feature to make UseForceUpdate callable directly | ||
// impl Fn<()> for UseForceUpdate { | ||
// extern "rust-call" fn call(&self, _args: ()) { | ||
// self.force_update() | ||
// } | ||
// } | ||
Comment on lines
+25
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should put this behind a |
||
|
||
/// This hook is used to manually force a function component to re-render. | ||
/// | ||
/// Try to use more specialized hooks, such as [`use_state`] and [`use_reducer`]. | ||
/// This hook should only be used when your component depends on external state where you | ||
/// can't subscribe to changes, or as a low-level primitive to enable such a subscription-based | ||
/// approach. | ||
/// | ||
/// For example, a large externally managed cache, such as a app-wide cache for GraphQL data | ||
/// should not rerender every component whenever new data arrives, but only those where a query | ||
/// changed. | ||
/// | ||
/// If the state of your component is not shared, you should need to use this hook. | ||
/// | ||
/// # Example | ||
/// | ||
/// This example implements a silly, manually updated display of the current time. The component | ||
/// is rerendered every time the button is clicked. You should usually use a timeout and `use_state` | ||
/// to automatically trigger a re-render every second without having to use this hook. | ||
/// | ||
/// ```rust | ||
/// # use yew::prelude::*; | ||
/// | ||
/// #[function_component] | ||
/// fn ManuallyUpdatedDate() -> Html { | ||
/// let trigger = use_force_update(); | ||
/// let onclick = use_state(move || Callback::from(move |_| trigger.force_update())); | ||
/// let last_update = js_sys::Date::new_0().to_utc_string(); | ||
/// html! { | ||
/// <div> | ||
/// <button onclick={&*onclick}>{"Update now!"}</button> | ||
/// <p>{"Last updated: "}{last_update}</p> | ||
/// </div> | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// [`use_state`]: super::use_state() | ||
/// [`use_reducer`]: super::use_reducer() | ||
pub fn use_force_update() -> impl Hook<Output = UseForceUpdate> { | ||
struct UseRerenderHook; | ||
|
||
impl Hook for UseRerenderHook { | ||
type Output = UseForceUpdate; | ||
|
||
fn run(self, ctx: &mut HookContext) -> Self::Output { | ||
UseForceUpdate { | ||
trigger: ctx.re_render.clone(), | ||
} | ||
} | ||
} | ||
|
||
UseRerenderHook | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
website/docs/concepts/function-components/hooks/use-force-update.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: 'use_force_update' | ||
--- | ||
|
||
`use_force_update` is a low-level hook specialized to triggering a re-render of a function component. | ||
Usually, this re-render is triggered automatically by the hook holding your data, for example when | ||
changing the data in a handle returned from [`use_state`](./use-state). | ||
|
||
::: caution | ||
|
||
Often, using this hook means you are doing something wrong and should use one of the other hooks, | ||
for example [`use_reducer`](./use-reducer) is a great way to track changing data. | ||
|
||
::: | ||
|
||
Use this hook when wrapping an API that doesn't expose precise subscription events for fetched data. | ||
You could then, at some point, invalidate your local cache of the fetched data and trigger a re-render | ||
to let the normal render flow of components tell you again which data to fetch, and repopulate the | ||
cache accordingly. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we follow
UseXXXHandle
convention and rename it toUseForceUpdateHandle
? likeUseReducerHandle
/UseStateHandle
etc