开发者

How to get a Vec<T> from a yew::UseStateHandle<Vec<T>>

开发者 https://www.devze.com 2022-12-07 18:50 出处:网络
use gloo::console::log; use std::ops::Deref; use web_sys::FileList; use yew::prelude::*; let filelist: UseStateHandle<Vec<FileList>> = use_state(|| vec![]);
use gloo::console::log;
use std::ops::Deref;
use web_sys::FileList;
use yew::prelude::*;


let filelist: UseStateHandle<Vec<FileList>> = use_state(|| vec![]);

Callback::from(move |event: DragEvent| {
    event.prevent_default();
    let files = event.data_transfer().unwrap().files();
    // log!(files);
    let 开发者_StackOverflow中文版cloned_filelist = filelist.clone();
    let mut filelistvalue = *cloned_filelist.deref().clone();
    // filelist_vec.push(files);   
})

*cloned_filelist.deref().clone() gives a slice [FileList] instead of Vec<FileList>

How can I use get a Vec from use_state and mutate it?


You can use to_vec for that:

use yew::prelude::*;
#[derive(Clone)]
struct FileList;
#[function_component(Main)]
fn main() -> Html {
    let filelist: UseStateHandle<Vec<FileList>> = use_state(|| vec![]);
    let files = FileList;
    let mut cloned: Vec<_> = filelist.to_vec();
    cloned.push(files);
    filelist.set(cloned);
    todo!()
}
0

精彩评论

暂无评论...
验证码 换一张
取 消