Yew: How can i access the local storage? (document.localStorage)

Created on 3 Jun 2020  路  2Comments  路  Source: yewstack/yew

Maybe I haven't found a library or any class that does this, could someone help me with this?
I actually found some documents online that seemed to be about that, but I couldn't quite understand.

I'm new to Rust and Yew even more! but I loved the possibilities and the tool.

question

Most helpful comment

You can do this using web-sys which provides bindings to the JavaScript Storage object.
You can find the documentation for that here.

Here's how you get access to the storage object from the window and then use it to get the value of a key.

// note the double unwrap because local_storage() returns Result<Option<...>, ...>
let local_storage = web_sys::window().unwrap().local_storage().unwrap().unwrap();

let value = local_storage.get_item("key").unwrap();

Note: If you're using yew-stdweb then you should use the stdweb equivalent.

All 2 comments

You can do this using web-sys which provides bindings to the JavaScript Storage object.
You can find the documentation for that here.

Here's how you get access to the storage object from the window and then use it to get the value of a key.

// note the double unwrap because local_storage() returns Result<Option<...>, ...>
let local_storage = web_sys::window().unwrap().local_storage().unwrap().unwrap();

let value = local_storage.get_item("key").unwrap();

Note: If you're using yew-stdweb then you should use the stdweb equivalent.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sackery picture sackery  路  3Comments

kellytk picture kellytk  路  3Comments

zethra picture zethra  路  5Comments

ghost picture ghost  路  4Comments

thienpow picture thienpow  路  3Comments