I’m new to rust (and yew) and I’ve built some components, but I guess I don’t fully understand the component lifecycle yet or rust lifetimes (though I’ve read through the docs and this SO answer). I have a component which takes a string as a property. The component renders a button that displays that string and passes that string to its onclick handler. If the string is empty, it should default to a provided default. I’m running into
error[E0521]: borrowed data escapes outside of closure
and
error[E0382]: borrow of moved value:
key
I think I understand why, but not how to fix it (new to rust).
fn create_storage_default() -> AttrValue {
AttrValue::from( ALL_STORAGE )
}
#[derive(Properties, PartialEq)]
pub struct ClearProps {
#[prop_or_else( create_storage_default )]
pub key: AttrValue,
}
const ALL_STORAGE: &str = "all storage";
#[function_component(ClearKey)]
pub fn clear_storage(props: &ClearProps) -> Html {
let key = props.key.clone();
let handle_click = Callback::from( move |key: &str| {
match key{
ALL_STORAGE => LocalStorage::clear(),
_ => LocalStorage::delete(key)
}
} );
html! {
<button
onclick={
let handle_click = handle_click.clone();
move |_| handle_click.emit(key.as_str() )
}
>
{format!("Clear {}", key )}
</button>
}
}