Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation for realistic full-stack apps #253

Open
ochrons opened this issue Apr 16, 2024 · 2 comments
Open

Improve documentation for realistic full-stack apps #253

ochrons opened this issue Apr 16, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation fullstack related to the fullstack crate

Comments

@ochrons
Copy link

ochrons commented Apr 16, 2024

Any realistic web app will have things like database connections, or calling other services on the server side. Due to Dioxus "hijacking" the main function and sharing it with browser and backend code, it's not easy to see how one would create database connection pools or RPC clients. Any documentation from axum or tokio is not very useful, with the way the main is setup in Dioxus.

Second problem is how to pass these DB pools or RPC clients to Dioxus server functions, so that they can be used to fetch data from external services. This should be possible via Axum's State mechanism, but since we're not dealing with Axum directly, this needs to be properly documented with good examples.

Right now the only somewhat related example is the auth example, but even that is not really documented at all.

@ealmloff
Copy link
Member

ealmloff commented Apr 18, 2024

Related issues in the main dioxus repo:

@ealmloff ealmloff added documentation Improvements or additions to documentation fullstack related to the fullstack crate labels Apr 18, 2024
@ochrons
Copy link
Author

ochrons commented May 8, 2024

Here's a simple example that will trip people (at least me!) when doing some real server calls inside closures

#[component]
pub fn Like(post_id: String) -> Element {
    let mut likes = use_signal(|| 0);
    rsx! {
        button {
            onclick: move |_| async move {
                let new_likes = like(post_id).await.unwrap();
                likes.set(new_likes);
            }
        }
        "{likes} likes"
    }
}

this will not compile due to moving post_id into the closure.

The fix is to own it at just the right spot:

#[component]
pub fn Like(post_id: String) -> Element {
    let mut likes = use_signal(|| 0);
    rsx! {
        button {
            onclick: move |_| {
                to_owned![post_id];
                async move {
                    let new_likes = like(post_id).await.unwrap();
                    likes.set(new_likes);
                }
        }
        "{likes} likes"
    }
}

Alternatively using post_id: ReadOnlySignal<String> also works without the owning part.

Would be good to document this common scenario. Current documentation or any of the examples don't cover this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation fullstack related to the fullstack crate
Projects
None yet
Development

No branches or pull requests

2 participants