Skip to content

Commit

Permalink
show #[builder] attribute in example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Sep 8, 2023
1 parent 00be3dd commit 1cfdec2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ async fn app() -> String {
<style>{s}</style>
</head>
<body>
<Section title="Hello".to_string()>
// call a component with no props
<Section />

// call a component with props and children
<Section title="Hello">
<Items />
</Section>
</body>
Expand All @@ -47,12 +51,17 @@ async fn app() -> String {
}

#[props]
/// mark a struct with #[props] to use it as props in a component.
/// #[builder] can customize single props, marking them as option or setting a default value.
struct SectionProps {
#[builder(setter(into), default = "Default Title".to_string())]
title: String,
#[builder(default)]
children: String,
}

#[component]
/// mark functions with #[component] to use them as components inside html! macro
fn Section(props: SectionProps) -> String {
html! {
<div>
Expand All @@ -62,7 +71,6 @@ fn Section(props: SectionProps) -> String {
}
}

// mark functions with #[component] to use them as components inside html! macro
#[component]
async fn Items() -> String {
let data = load_data_async().await;
Expand All @@ -78,7 +86,8 @@ async fn Items() -> String {
}
}

// async functions can be easily used in the body of a component
/// async functions can be easily used in the body of a component, as every component is an async
/// function
async fn load_data_async() -> Vec<String> {
vec!["a".to_string(), "b".to_string(), "c".to_string()]
}
Expand Down
15 changes: 12 additions & 3 deletions rscx/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ async fn app() -> String {
<style>{s}</style>
</head>
<body>
<Section title="Hello".to_string()>
// call a component with no props
<Section />

// call a component with props and children
<Section title="Hello">
<Items />
</Section>
</body>
Expand All @@ -27,12 +31,17 @@ async fn app() -> String {
}

#[props]
/// mark a struct with #[props] to use it as props in a component.
/// #[builder] can customize single props, marking them as option or setting a default value.
struct SectionProps {
#[builder(setter(into), default = "Default Title".to_string())]
title: String,
#[builder(default)]
children: String,
}

#[component]
/// mark functions with #[component] to use them as components inside html! macro
fn Section(props: SectionProps) -> String {
html! {
<div>
Expand All @@ -42,7 +51,6 @@ fn Section(props: SectionProps) -> String {
}
}

// mark functions with #[component] to use them as components inside html! macro
#[component]
async fn Items() -> String {
let data = load_data_async().await;
Expand All @@ -58,7 +66,8 @@ async fn Items() -> String {
}
}

// async functions can be easily used in the body of a component
/// async functions can be easily used in the body of a component, as every component is an async
/// function
async fn load_data_async() -> Vec<String> {
vec!["a".to_string(), "b".to_string(), "c".to_string()]
}

0 comments on commit 1cfdec2

Please sign in to comment.