mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
Add old props to fn changed() (#2851)
* Adding old_props to fn changed() * Change old_props type to &Self::Properties * Update examples that use fn changed() and want old_props * Fix tests * Fix invalid test flags * cargo fmt * Oops wrong regex * Revert change for older version * Remove unnecessary clone * Add migration guide * Oh wow the test was not running xD
This commit is contained in:
parent
7c12a6bf32
commit
c7b4029e1b
@ -25,7 +25,6 @@ pub struct Props {
|
||||
pub struct Simulation {
|
||||
boids: Vec<Boid>,
|
||||
interval: Interval,
|
||||
settings: Settings,
|
||||
generation: usize,
|
||||
}
|
||||
impl Component for Simulation {
|
||||
@ -49,7 +48,6 @@ impl Component for Simulation {
|
||||
Self {
|
||||
boids,
|
||||
interval,
|
||||
settings,
|
||||
generation,
|
||||
}
|
||||
}
|
||||
@ -73,10 +71,10 @@ impl Component for Simulation {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, old_props: &Self::Properties) -> bool {
|
||||
let props = ctx.props();
|
||||
let should_reset = self.settings != props.settings || self.generation != props.generation;
|
||||
self.settings = props.settings.clone();
|
||||
let should_reset =
|
||||
old_props.settings != props.settings || self.generation != props.generation;
|
||||
self.generation = props.generation;
|
||||
if should_reset {
|
||||
self.boids.clear();
|
||||
|
||||
@ -23,7 +23,7 @@ impl Component for AuthorCard {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.author = Author::generate_from_seed(ctx.props().seed);
|
||||
true
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ impl Component for PostCard {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.post = PostMeta::generate_from_seed(ctx.props().seed);
|
||||
true
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ impl Component for Author {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.author = content::Author::generate_from_seed(ctx.props().seed);
|
||||
true
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ impl Component for Post {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.post = content::Post::generate_from_seed(ctx.props().seed);
|
||||
true
|
||||
}
|
||||
|
||||
@ -298,7 +298,7 @@ impl FunctionComponent {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn changed(&mut self, _ctx: &::yew::html::Context<Self>) -> ::std::primitive::bool {
|
||||
fn changed(&mut self, _ctx: &::yew::html::Context<Self>, _old_props: &Self::Properties) -> ::std::primitive::bool {
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
@ -91,7 +91,7 @@ impl<T: Clone + PartialEq + 'static> Component for ContextProvider<T> {
|
||||
}
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
let props = ctx.props();
|
||||
let should_render = if self.children == props.children {
|
||||
false
|
||||
|
||||
@ -314,7 +314,7 @@ mod tests {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, _ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
|
||||
@ -205,8 +205,8 @@ where
|
||||
};
|
||||
|
||||
if self.context.props != props {
|
||||
self.context.props = props;
|
||||
self.component.changed(&self.context)
|
||||
let old_props = std::mem::replace(&mut self.context.props, props);
|
||||
self.component.changed(&self.context, &*old_props)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@ -791,7 +791,7 @@ mod tests {
|
||||
false
|
||||
}
|
||||
|
||||
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, _ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
@ -851,7 +851,7 @@ mod tests {
|
||||
msg
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
self.lifecycle = Rc::clone(&ctx.props().lifecycle);
|
||||
self.lifecycle.borrow_mut().push("change".into());
|
||||
false
|
||||
|
||||
@ -100,7 +100,7 @@ pub trait BaseComponent: Sized + 'static {
|
||||
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool;
|
||||
|
||||
/// React to changes of component properties.
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool;
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool;
|
||||
|
||||
/// Returns a component layout to be rendered.
|
||||
fn view(&self, ctx: &Context<Self>) -> HtmlResult;
|
||||
@ -154,7 +154,7 @@ pub trait Component: Sized + 'static {
|
||||
///
|
||||
/// By default, this function will return true and thus make the component re-render.
|
||||
#[allow(unused_variables)]
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
@ -206,8 +206,8 @@ where
|
||||
Component::update(self, ctx, msg)
|
||||
}
|
||||
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool {
|
||||
Component::changed(self, ctx)
|
||||
fn changed(&mut self, ctx: &Context<Self>, old_props: &Self::Properties) -> bool {
|
||||
Component::changed(self, ctx, old_props)
|
||||
}
|
||||
|
||||
fn view(&self, ctx: &Context<Self>) -> HtmlResult {
|
||||
@ -228,6 +228,7 @@ where
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(any(feature = "ssr", feature = "csr"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@ -258,6 +259,6 @@ mod tests {
|
||||
prepared_state: None,
|
||||
};
|
||||
assert!(Component::update(&mut comp, &ctx, ()));
|
||||
assert!(Component::changed(&mut comp, &ctx));
|
||||
assert!(Component::changed(&mut comp, &ctx, &Rc::new(())));
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ impl Component for Comp {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
|
||||
fn changed(&mut self, _ctx: &Context<Self>, _: &Self::Properties) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
|
||||
@ -58,3 +58,27 @@ will result in a compile error
|
||||
Previously node ref passed to a component was bound to the first element rendered by it.
|
||||
If this behavior is still desired, it is recommended to use add a `r#ref` field to the
|
||||
component's properties and bind it manually
|
||||
|
||||
## `changed` Method on Components
|
||||
|
||||
The method `fn changed()` has now a new argument to provide the old properties
|
||||
to the function.
|
||||
|
||||
The old method's signature was:
|
||||
|
||||
```rust ,ignore
|
||||
fn changed(&mut self, ctx: &Context<Self>) -> bool
|
||||
```
|
||||
|
||||
The new method's signature is now:
|
||||
|
||||
```rust ,ignore
|
||||
fn changed(&mut self, ctx: &Context<Self>, old_props: &Self::Properties) -> bool
|
||||
```
|
||||
|
||||
This can be adjusted automatically in your code using this bash script (save
|
||||
your code before running this!):
|
||||
|
||||
```bash
|
||||
perl -p -i -e 's/fn changed\(&mut self, (\w+): &Context<Self>\)/fn changed(&mut self, $1: &Context<Self>, _old_props: &Self::Properties)/g' $(find . -name \*.rs)
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user