Files
dioxus/packages/router/src/components/router.rs
T
Evan AlmloffandJonathan Kelley 281087469a Move the history provider into the context (#3048)
* move history providers into a separate crate

* start moving route providers into the renderers

* clean up intoroutable

* remove into routable

* fix router tests

* Provide history providers in each renderer

* implement nested routers

* move the lens out of the history crate

* re-export dioxus history trait in the prelude

* also re-export the history function

* fix history doctests

* some light cleanups

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
2024-10-31 12:44:04 -07:00

59 lines
1.3 KiB
Rust

use dioxus_lib::prelude::*;
use std::str::FromStr;
use crate::{
prelude::{provide_router_context, Outlet},
routable::Routable,
router_cfg::RouterConfig,
};
/// The props for [`Router`].
#[derive(Props)]
pub struct RouterProps<R: Clone + 'static> {
#[props(default, into)]
config: Callback<(), RouterConfig<R>>,
}
impl<T: Clone> Clone for RouterProps<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: Clone> Copy for RouterProps<T> {}
impl<R: Clone + 'static> Default for RouterProps<R> {
fn default() -> Self {
Self {
config: Callback::new(|_| RouterConfig::default()),
}
}
}
impl<R: Clone> PartialEq for RouterProps<R> {
fn eq(&self, _: &Self) -> bool {
// prevent the router from re-rendering when the initial url or config changes
true
}
}
/// A component that renders the current route.
pub fn Router<R: Routable + Clone>(props: RouterProps<R>) -> Element
where
<R as FromStr>::Err: std::fmt::Display,
{
use crate::prelude::{outlet::OutletContext, RouterContext};
use_hook(|| {
provide_router_context(RouterContext::new(props.config.call(())));
provide_context(OutletContext::<R> {
current_level: 0,
_marker: std::marker::PhantomData,
});
});
rsx! { Outlet::<R> {} }
}