wip: move examples around

This commit is contained in:
Jonathan Kelley committed 2021-07-11 18:39:45 -04:00
1 parent 269e81b0fd
commit 304259d818
24 files changed
+81 -71

No files matched your search

+2 -2
View File
@@ -85,7 +85,7 @@ static AntipatternNestedFragments: FC<()> = |cx| {
/// recognize from the function signature, but Dioxus will not update the "live" version of state. Calling `set_state`
/// merely places a new value in the queue and schedules the component for a future update.
static AntipaternRelyingOnSetState: FC<()> = |cx| {
let (state, set_state) = use_state_classic(cx, || "Hello world");
let (state, set_state) = use_state(cx, || "Hello world").classic();
set_state("New state");
// This will return false! `state` will *still* be "Hello world"
assert!(state == &"New state");
@@ -126,7 +126,7 @@ static AntipatternMisusedHooks: FC<MisuedHooksProps> = |cx| {
if cx.should_render_state {
// do not place a hook in the conditional!
// prefer to move it out of the conditional
let (state, set_state) = use_state_classic(cx, || "hello world");
let (state, set_state) = use_state(cx, || "hello world").classic();
rsx!(in cx, div { "{state}" })
} else {
rsx!(in cx, div { "Not rendering state" })
+2 -6
View File
@@ -3,10 +3,6 @@
//!
//! This is a simple pattern that allows you to return no elements!
use dioxus::prelude::*;
fn main() {}
static Example: FC<()> = |cx| {
//
cx.render(rsx! {})
};
use dioxus::prelude::*;
static Example: FC<()> = |cx| cx.render(rsx! { Fragment {} });
+7 -6
View File
@@ -35,11 +35,12 @@ static Example2: FC<()> = |cx| {
// Using the `fragment` method on the NodeFactory
static Example3: FC<()> = |cx| {
cx.render(LazyNodes::new(move |fac: &NodeFactory| {
fac.fragment_builder(None, |list| {
list.add_child(fac.text(format_args!("A")))
.add_child(fac.text(format_args!("B")))
.finish()
})
cx.render(LazyNodes::new(move |fac| {
fac.fragment_from_iter([
fac.text(format_args!("A")),
fac.text(format_args!("B")),
fac.text(format_args!("A")),
fac.text(format_args!("B")),
])
}))
};
+2 -2
View File
@@ -2,11 +2,11 @@ use dioxus::prelude::*;
fn main() {}
static Example: FC<()> = |cx| {
let (g, set_g) = use_state_classic(cx, || 0);
let g = use_state(cx, || 0);
let v = (0..10).map(|f| {
rsx! {
li {
onclick: move |_| set_g(10)
onclick: move |_| g.set(10)
}
}
});
+2 -2
View File
@@ -11,11 +11,11 @@ fn main() {}
/// We can use `set_name` in multiple closures; the closures automatically *copy* the reference to set_name.
static ButtonList: FC<()> = |cx| {
let (name, set_name) = use_state_classic(cx, || "...?");
let name = use_state(cx, || "...?");
let names = ["jack", "jill", "john", "jane"]
.iter()
.map(move |n| rsx!(button { onclick: move |_| set_name(n), "{n}" }));
.map(move |n| rsx!(button { onclick: move |_| name.set(n), "{n}" }));
cx.render(rsx!(
div {
+23 -19
View File
@@ -16,7 +16,7 @@ struct DogApi {
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
pub static App: FC<()> = |cx| {
let doggo = use_future_effect(&cx, move || async move {
let doggo = use_future_effect(cx, move || async move {
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
@@ -31,29 +31,33 @@ pub static App: FC<()> = |cx| {
))
};
use dioxus_core::virtual_dom::SuspendedContext;
// use dioxus_core::virtual_dom::SuspendedContext;
use futures::Future;
use futures::FutureExt;
use std::pin::Pin;
fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
cx: &impl Scoped<'a>,
url: &str,
g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
) -> VNode<'a> {
// essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
let fetch_promise = cx.use_hook(
move || surf::get(url).recv_json::<T>().boxed_local(),
// just pass the future through
|p| p,
|_| (),
);
cx.suspend(fetch_promise, g)
fn use_fetch<'a, T, P, F>(cx: Context<P>, url: &str, g: F) -> VNode<'a>
where
T: serde::de::DeserializeOwned + 'static,
// F: FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
F: FnOnce((), surf::Result<T>) -> VNode<'a> + 'a,
{
todo!()
// // essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
// let fetch_promise = cx.use_hook(
// move || surf::get(url).recv_json::<T>().boxed_local(),
// // just pass the future through
// |p| p,
// |_| (),
// );
// cx.suspend(fetch_promise, g)
}
/// Spawns the future only when the inputs change
fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
cx: &impl Scoped<'a>,
g: impl FnOnce() -> F + 'a,
) -> VNode<'a> {
fn use_future_effect<'a, 'b, F, P, F2>(cx: Context<P>, g: F2) -> VNode<'a>
where
F: Future<Output = VNode<'b>>,
F2: FnOnce() -> F + 'a,
{
todo!()
}
+11 -4
View File
@@ -14,8 +14,15 @@ use dioxus::{builder::ElementBuilder, prelude::NodeFactory};
fn main() {}
// TODO
struct MyEle<'a, 'b> {
el: ElementBuilder<'a, 'b>,
fac: &'b NodeFactory<'a>,
mod dioxus_elements {
use dioxus::prelude::DioxusElement;
struct custom_element;
impl DioxusElement for custom_element {
const TAG_NAME: &'static str = "custom_element";
const NAME_SPACE: Option<&'static str> = None;
}
// Re-export the normal html namespace
pub use dioxus::prelude::dioxus_elements::*;
}