DioxusLabs / DioxusLabs/dioxus

Option to create dioxus Router with AppState

Open
#5,217 0 comments 3 reactions 0 assignees View on GitHub
enhancement fullstack
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

## Feature Request

Right now it's not easy to add app state in the main module because `dioxus::server::router` returns Router<()>.

## Implement Suggestion

Current implementation:
```rust
pub fn router(app: fn() -> Element) -> Router {
let cfg = ServeConfig::new();
apply_base_path(
Router::new().serve_dioxus_application(cfg.clone(), app),
app,
cfg,
base_path().map(|s| s.to_string()),
)
}
```

We can change the current function to accept the State type:
```rust
pub fn router(app: fn() -> Element) -> Router {
let cfg = ServeConfig::new();
apply_base_path(
Router::new().serve_dioxus_application(cfg.clone(), app),
app,
cfg,
base_path().map(|s| s.to_string()),
)
}
```

Or create a new helper function (to not break current usage) like this:
```rust
pub fn router_with_state(app: fn() -> Element) -> Router {
let cfg = ServeConfig::new();
apply_base_path(
Router::new().serve_dioxus_application(cfg.clone(), app),
app,
cfg,
base_path().map(|s| s.to_string()),
)
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.