Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for LaunchBuilder methods #3629

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion packages/dioxus/src/launch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! Launch helper macros for fullstack apps
#![allow(clippy::new_without_default)]
#![allow(unused)]
use dioxus_config_macro::*;
Expand Down Expand Up @@ -131,6 +130,27 @@ impl LaunchBuilder {

impl LaunchBuilder {
/// Inject state into the root component's context that is created on the thread that the app is launched on.
///
/// # Example
/// ```rust, no_run
/// use dioxus::prelude::*;
/// use std::any::Any;
///
/// #[derive(Default)]
/// struct MyState {
/// value: i32,
/// }
///
/// fn app() -> Element {
/// rsx! {
/// div { "Hello, world!" }
/// }
/// }
///
/// dioxus::LaunchBuilder::new()
/// .with_context_provider(|| Box::new(MyState { value: 42 }))
/// .launch(app);
/// ```
pub fn with_context_provider(
mut self,
state: impl Fn() -> Box<dyn Any> + Send + Sync + 'static,
Expand All @@ -140,6 +160,27 @@ impl LaunchBuilder {
}

/// Inject state into the root component's context.
///
/// # Example
/// ```rust, no_run
/// use dioxus::prelude::*;
/// use std::any::Any;
///
/// #[derive(Clone)]
/// struct MyState {
/// value: i32,
/// }
///
/// fn app() -> Element {
/// rsx! {
/// div { "Hello, world!" }
/// }
/// }
///
/// dioxus::LaunchBuilder::new()
/// .with_context(MyState { value: 42 })
/// .launch(app);
/// ```
pub fn with_context(mut self, state: impl Any + Clone + Send + Sync + 'static) -> Self {
self.contexts
.push(Box::new(move || Box::new(state.clone())));
Expand All @@ -149,6 +190,22 @@ impl LaunchBuilder {

impl LaunchBuilder {
/// Provide a platform-specific config to the builder.
///
/// # Example
/// ```rust, no_run
/// use dioxus::prelude::*;
/// use dioxus_desktop::Config;
///
/// fn app() -> Element {
/// rsx! {
/// div { "Hello, world!" }
/// }
/// }
///
/// dioxus::LaunchBuilder::desktop()
/// .with_cfg(Config::new().with_window(|w| w.with_title("My App")))
/// .launch(app);
/// ```
pub fn with_cfg(mut self, config: impl LaunchConfig) -> Self {
self.configs.push(Box::new(config));
self
Expand Down
Loading