#Have a lifetime problem

1 messages · Page 1 of 1 (latest)

scenic oxide
#

lifetime may not live long enough
#[function_component]
pub fn some_component(_props: &OrderInputProp) -> Html {
...
let t = use_state(|| _props);
....

}
^^^^^^^^^^^^^^ returning this value requires that '1 must outlive 'static

I will happy for a tip how i can resolve this problem

bleak leaf
#

well use_state does in fact use an FnOnce, unlike some other hooks, but the result (T: 'static) is stored away in the component, so you cannot store a props: & in the component, as that & (rust compiler gives it a name to explain the error for you, so it becomes &'1) lives only as long as the call does

#

if you really want to store the props struct in that state, try use_state(|| _props.clone()) (but ensure OrderInputProp is Clone) - this will turn a &OrderInputProp into a OrderInputProp, which does not have any lifetime limitations to it

scenic oxide
#

i have tryed with .clone() but it do not work.

bleak leaf
scenic oxide
#

the same.: returning this value requires that '1 must outlive 'static

#

Found th problem the clone derive was missing at the struct sorry 🙈

bleak leaf
#

i guess trait Copy: Clone and impl<T> Copy for &T kicks in when struct is not Clone

#

but i did mention that

but ensure OrderInputProp is Clone