Skip to content

Commit

Permalink
docs: Update the book guides (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 authored Jun 1, 2024
1 parent 1b6a331 commit 2671d6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
20 changes: 11 additions & 9 deletions book/src/dioxus.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ fn MyComponent(name: String, age: u8) -> Element {

You can as well have a separate Struct for the props:
```rs, no_run
struct MyComponentProps {
name: String,
age: u8
Expand All @@ -98,28 +97,31 @@ Dioxus built-in state management uses **Signals**, and they are usually created
```rs, no_run
fn MyComponent() -> Element {
// `use_signal` takes a callback that initializes the state
// No matter how many times the component re runs,
// the initialization callback will only run once at the first component run
let mut state = use_signal(|| 0);
// Because signals are copy, we can move them into closures
let onclick = move |_| {
// Signals provide some shortcuts for certain types
// Signals provide some mutation shortcuts for certain types
state += 1;
// But we could do as well
*state.write() += 1;
};
// You can subscribe to a signal, by calling it (`signal()`),
// calling the `signal.read()` method, or just embedding it into the RSX.
// You subscribe to a signal by calling it (`signal()`),
// calling the `read()` method, or just embedding it into the RSX.
// Everytime the signal is mutated the component function will rerun
// because it has been subscribed, and thus producing a
// new Element with the updated counter.
// because it has been subscribed, this will end up producing a
// new `Element` with the updated counter.
println!("{}", state());
println!("{}", state.read());
rsx!(
label {
label {
onclick,
"State is {value}"
}
"State is {state}"
}
)
}
```
Expand Down
28 changes: 17 additions & 11 deletions book/src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cargo init

### Cargo.toml

Make sure to add Freya and Dioxus as dependencies:
Let's add Freya and Dioxus, but this last one with only 2 features selected as we don't want to pull unnecessary dependencies.

```toml
[package]
Expand All @@ -29,7 +29,7 @@ dioxus = { version = "0.5", features = ["macro", "hooks"], default-features = fa

### src/main.rs

In Freya, you run your app by calling a `launch` method and passing it your root Component:
You pass your root (usually named `app`) component to the `launch` function, which will open the window and then render your component.

```rust, no_run
#![cfg_attr(
Expand All @@ -40,43 +40,49 @@ In Freya, you run your app by calling a `launch` method and passing it your root
use freya::prelude::*;
fn main() {
launch(app); // This will block the main thread
launch(app); // Be aware that this will block the thread
}
```

Let's define our root component, called `app`:
Let's define our root component:

```rust, no_run
fn app() -> Element {
// RSX is a special syntax to define the UI of our components
// Here we simply show a label element with some text
rsx!(
label { "Hello, World!" }
)
}
```

Components in Freya/Dioxus are simply functions that return an `Element` and might receive some properties as arguments.
Components in Freya are just functions that return an `Element` and might receive some properties as arguments.

Let's make it stateful by using Dioxus Signals:
Let's make it stateful by using Signals from Dioxus:

```rust, no_run
fn app() -> Element {
// `use_signal` takes a callback that initializes the state
// No matter how many times the component re runs,
// the initialization callback will only run once at the first component run
let mut state = use_signal(|| 0);
// Because signals are copy, we can move them into closures
let onclick = move |_| {
// Signals provide some shortcuts for certain types
// Signals provide some mutation shortcuts for certain types
state += 1;
// But we could do as well
*state.write() += 1;
};
// You can subscribe to a signal, by calling it (`signal()`),
// calling the `signal.read()` method, or just embedding it into the RSX.
// You subscribe to a signal by calling it (`signal()`),
// calling the `read()` method, or just embedding it into the RSX.
// Everytime the signal is mutated the component function will rerun
// because it has been subscribed, and thus producing a
// new Element with the updated counter.
// because it has been subscribed, this will end up producing a
// new `Element` with the updated counter.
println!("{}", state());
println!("{}", state.read());
rsx!(
label {
Expand Down

0 comments on commit 2671d6f

Please sign in to comment.