Provide global HttpClient to use_future #832
Answered
by
ealmloff
patrick-dedication
asked this question in
Q&A
-
Hi, first off, thanks for making Rust web so great.
Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
ealmloff
Feb 19, 2023
Replies: 1 comment 6 replies
-
You can use a Lazy static variable to initialize FetchHttpClient: static CLIENT: once_cell::sync::Lazy = once_cell::sync::Lazy(|| FetchHttpClient::new("http://127.0.0.1:8000"));
#[allow(non_snake_case)]
pub fn App(cx: Scope) -> Element {
let slides = use_slides(cx);
cx.render(match slides.value() {
Some(slides) => rsx! { Slideshow { slides: slides } },
None => rsx! { div { "Loading slides..." } },
})
} pub fn use_slides(cx: &ScopeState) -> &UseFuture<Slides> {
use_future(cx, (), |_| async move { get_slides(&CLIENT).await })
} |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
patrick-dedication
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use a Lazy static variable to initialize FetchHttpClient: