update doc examples

This commit is contained in:
Evan Almloff committed 2023-05-31 15:51:26 -05:00
1 parent 35befa1784
commit 09cabe4e8b
22 files changed
+474 -518

No files matched your search

+18 -27
View File
@@ -1,17 +1,13 @@
use dioxus::prelude::ScopeState;
use crate::{routable::Routable, utils::use_router_internal::use_router_internal};
use crate::prelude::*;
use crate::utils::use_router_internal::use_router_internal;
/// A hook that provides access to information about the current routing location.
///
/// # Return values
/// - [`RouterError::NotInsideRouter`], when the calling component is not nested within another
/// component calling the [`use_router`] hook.
/// - Otherwise [`Ok`].
///
/// # Important usage information
/// Make sure to [`drop`] the returned [`RwLockReadGuard`] when done rendering. Otherwise the router
/// will be frozen.
/// - None, when not called inside a [`GenericRouter`] component.
/// - Otherwise the current route.
///
/// # Panic
/// - When the calling component is not nested within another component calling the [`use_router`]
@@ -20,28 +16,25 @@ use crate::{routable::Routable, utils::use_router_internal::use_router_internal}
/// # Example
/// ```rust
/// # use dioxus::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use dioxus_router::{history::*, prelude::*};
/// fn App(cx: Scope) -> Element {
/// use_router(
/// &cx,
/// &|| RouterConfiguration {
/// synchronous: true, // asynchronicity not needed for doc test
/// history: Box::new(MemoryHistory::with_initial_path("/some/path").unwrap()),
/// ..Default::default()
/// },
/// &|| Segment::empty()
/// );
///
/// #[derive(Clone, Serialize, Deserialize, Routable)]
/// enum Route {
/// #[route("/")]
/// Index {},
/// }
///
/// fn App(cx: Scope) -> Element {
/// render! {
/// h1 { "App" }
/// Content { }
/// Router {}
/// }
/// }
///
/// fn Content(cx: Scope) -> Element {
/// let state = use_route(&cx)?;
/// let path = state.path.clone();
///
/// #[inline_props]
/// fn Index(cx: Scope) -> Element {
/// let path = use_route(&cx).unwrap();
/// render! {
/// h2 { "Current Path" }
/// p { "{path}" }
@@ -50,11 +43,9 @@ use crate::{routable::Routable, utils::use_router_internal::use_router_internal}
/// #
/// # let mut vdom = VirtualDom::new(App);
/// # let _ = vdom.rebuild();
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><h2>Current Path</h2><p>/some/path</p>")
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><h2>Current Path</h2><p>/</p>")
/// ```
///
/// [`use_router`]: crate::hooks::use_router
pub fn use_route<R: Routable + Clone>(cx: &ScopeState) -> Option<R> {
pub fn use_generic_route<R: Routable + Clone>(cx: &ScopeState) -> Option<R> {
match use_router_internal(cx) {
Some(r) => Some(r.current()),
None => {
+44 -1
View File
@@ -5,7 +5,50 @@ use crate::{
utils::use_router_internal::use_router_internal,
};
/// A hook that provides access to information about the router
/// A hook that provides access to information about the router. The Router will define a version of this hook with an explicit type.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # use dioxus_router::{history::*, prelude::*};
/// # use serde::{Deserialize, Serialize};
/// #[derive(Clone, Serialize, Deserialize, Routable)]
/// enum Route {
/// #[route("/")]
/// Index {},
/// #[route("/:id")]
/// Dynamic { id: usize },
/// }
///
/// fn App(cx: Scope) -> Element {
/// render! {
/// Router {}
/// }
/// }
///
/// #[inline_props]
/// fn Index(cx: Scope) -> Element {
/// let router = use_router(&cx);
///
/// render! {
/// button {
/// onclick: move |_| { router.push(Route::Dynamic { id: 1234 }); },
/// "Go to /1234"
/// }
/// }
/// }
///
/// #[inline_props]
/// fn Dynamic(cx: Scope, id: usize) -> Element {
/// render! {
/// p {
/// "Current ID: {id}"
/// }
/// }
/// }
///
/// # let mut vdom = VirtualDom::new(App);
/// # let _ = vdom.rebuild();
/// ```
pub fn use_generic_router<R: Routable + Clone>(cx: &ScopeState) -> &GenericRouterContext<R> {
use_router_internal(cx)
.as_ref()