chore: finish tweaking context api

This commit is contained in:
Jonathan Kelley committed 2022-12-05 15:30:49 -08:00
1 parent 99a818996e
commit df4a1b6c7a
12 files changed
+76 -81

No files matched your search

+2 -4
View File
@@ -1,6 +1,4 @@
use std::sync::Arc;
use crate::{use_route, RouterCore};
use crate::{use_route, RouterContext};
use dioxus::prelude::*;
/// Props for the [`Link`](struct.Link.html) component.
@@ -77,7 +75,7 @@ pub struct LinkProps<'a> {
/// }
/// ```
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
let svc = cx.use_hook(|| cx.consume_context::<Arc<RouterCore>>());
let svc = use_context::<RouterContext>(cx);
let LinkProps {
to,
+6 -10
View File
@@ -1,7 +1,5 @@
use crate::{RouteContext, RouterContext};
use dioxus::prelude::*;
use std::sync::Arc;
use crate::{RouteContext, RouterCore};
/// Props for the [`Route`](struct.Route.html) component.
#[derive(Props)]
@@ -27,15 +25,13 @@ pub struct RouteProps<'a> {
/// )
/// ```
pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
let router_root = cx
.use_hook(|| cx.consume_context::<Arc<RouterCore>>())
.as_ref()
.unwrap();
let router_root = use_context::<RouterContext>(cx).unwrap();
let root_context = use_context::<RouteContext>(cx);
cx.use_hook(|| {
// create a bigger, better, longer route if one above us exists
let total_route = match cx.consume_context::<RouteContext>() {
Some(ctx) => ctx.total_route,
let total_route = match root_context {
Some(ctx) => ctx.total_route.clone(),
None => cx.props.to.to_string(),
};
@@ -46,7 +42,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
});
// submit our rout
router_root.register_total_route(route_context.total_route, cx.scope_id());
router_root.register_total_route(route_context.total_route.clone(), cx.scope_id());
});
log::trace!("Checking Route: {:?}", cx.props.to);
+5 -6
View File
@@ -1,6 +1,5 @@
use crate::{cfg::RouterCfg, RouterCore};
use crate::{cfg::RouterCfg, RouterContext, RouterService};
use dioxus::prelude::*;
use std::sync::Arc;
/// The props for the [`Router`](fn.Router.html) component.
#[derive(Props)]
@@ -21,7 +20,7 @@ pub struct RouterProps<'a> {
///
/// This lets you easily implement redirects
#[props(default)]
pub onchange: EventHandler<'a, Arc<RouterCore>>,
pub onchange: EventHandler<'a, RouterContext>,
/// Set the active class of all Link components contained in this router.
///
@@ -40,15 +39,15 @@ pub struct RouterProps<'a> {
/// Will fallback to HashRouter is BrowserRouter is not available, or through configuration.
#[allow(non_snake_case)]
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
let svc = cx.use_hook(|| {
cx.provide_context(RouterCore::new(
let svc = use_context_provider(cx, || {
RouterService::new(
&cx,
RouterCfg {
base_url: cx.props.base_url.map(|s| s.to_string()),
active_class: cx.props.active_class.map(|s| s.to_string()),
initial_url: cx.props.initial_url.clone(),
},
))
)
});
// next time we run the rout_found will be filled