Hi everyone
I am trying to build a simple application similar to Dontpad but in yew.
The main thing that this component needs to do is to send a request every 5 seconds to the server ,get response , and then update the textarea in the page.
here is my code:
pub struct Props{
pub url:String,
#[prop_or_else(||"".to_owned())]
inner_text:String
}
pub struct Page{
pub text:String
}
impl Component for Page {
type Message=();
type Properties=Props;
fn create(ctx: &Context<Self>) -> Self {
log!(ctx.props().url.clone());
let url = ctx.deref().props().url.clone();
log!(&url);
let mut textarea_text="".to_string();
let interval = Interval::new(5000,move|| {send_request(ctx, url)} ); //Error
interval.forget();
Self{text:"".to_string()}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<>
<ToolBar url={"test"}/>
<div>
<div class="container-fluid">
<textarea id="textarea" class={classes!("textarea",css!("white-space:pre-wrap;"))} value={ctx.props().inner_text.clone()} >
</textarea>
</div>
</div>
</>
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
false
}
}
pub fn send_request(ctx:&Context<Page>,url:String){
let url = url.clone();
wasm_bindgen_futures::spawn_local(async move{
let _url = format!("http://127.0.0.1:3000/{}",url);
// log!(&_url);
// log!("sdfasdfas");
let response = Request::get(_url.as_str())
.send()
.await
.unwrap()
.text()
.await
.unwrap();
/*
need to change textarea value to response here
*/
ctx.props().inner_text=response; //Error
});
}
as you might know due to life time issues i can not pass ctx
to send_request
function and I am getting
borrowed data escapes outside of associated function
`ctx` escapes the associated function body here
error
so I am struggling with this particular problem and I don't know how to solve it.
Anyone has an idea?