Skip to content

Commit 3963146

Browse files
committed
feat: function to get router out of dom
1 parent 3c99200 commit 3963146

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

packages/router/src/service.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// does each window have its own router? probably, lol
33

44
use crate::cfg::RouterCfg;
5-
use dioxus_core::{ScopeId, ScopeState};
5+
use dioxus_core::{ScopeId, ScopeState, VirtualDom};
66
use std::any::Any;
77
use std::sync::Weak;
88
use std::{
@@ -103,6 +103,13 @@ impl RouterCore {
103103
svc
104104
}
105105

106+
/// Push a new route with no custom title or serialized state.
107+
///
108+
/// This is a convenience method for easily navigating.
109+
pub fn navigate_to(&self, route: &str) {
110+
self.push_route(route, None, None);
111+
}
112+
106113
/// Push a new route to the history.
107114
///
108115
/// This will trigger a route change event.
@@ -221,6 +228,20 @@ impl RouterCore {
221228
}
222229
}
223230

231+
/// Get the router service from an existing VirtualDom.
232+
///
233+
/// Takes an optional target_scope parameter to specify the scope to use if ScopeId is not the component
234+
/// that owns the router.
235+
///
236+
/// This might change in the future.
237+
pub fn get_router_from_vdom(
238+
dom: &VirtualDom,
239+
target_scope: Option<ScopeId>,
240+
) -> Option<Arc<RouterCore>> {
241+
dom.get_scope(target_scope.unwrap_or(ScopeId(0)))
242+
.and_then(|scope| scope.consume_context::<Arc<RouterCore>>())
243+
}
244+
224245
fn clean_route(route: String) -> String {
225246
if route.as_str() == "/" {
226247
return route;

0 commit comments

Comments
 (0)