Skip to content

Global AppState in server functions #2367

Closed Answered by gbj
Tibuarka asked this question in Q&A
Feb 25, 2024 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

I don't think you can add multiple kinds of state like that, although I'm surprised Axum lets it compile. I am not an expert with Axum by any means.

But I put together a working example like this, using a single state type and the FromRef pattern in Axum:

app_state.rs

use axum::extract::FromRef;
use leptos::LeptosOptions;

#[derive(FromRef, Clone, Debug)]
pub struct AppState {
    pub title: &'static str,
    pub leptos_options: LeptosOptions,
}

main.rs

    let app_state = AppState {
        title: "Foo",
        leptos_options,
    };

    // build our application with a route
    let app = Router::new()
        .leptos_routes(&app_state, routes, App)
        .fallback(file_and_error_han…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Tibuarka
Comment options

@Tibuarka
Comment options

@gbj
Comment options

gbj Feb 26, 2024
Maintainer

Answer selected by Tibuarka
@Tibuarka
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants