dioxus/packages/core/README.md

94 lines
3.1 KiB
Markdown
Raw Normal View History

2022-11-30 15:31:44 +00:00
# dioxus-core
2021-01-14 07:56:41 +00:00
2024-01-30 01:02:26 +00:00
`dioxus-core` provides a fast and featureful VirtualDom implementation for Rust.
2021-01-14 07:56:41 +00:00
2024-01-30 01:02:26 +00:00
```rust, ignore
use dioxus_core::prelude::*;
2024-01-30 01:02:26 +00:00
let vdom = VirtualDom::new(app);
let real_dom = SomeRenderer::new();
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
loop {
select! {
evt = real_dom.event() => vdom.handle_event(evt),
_ = vdom.wait_for_work() => {}
}
vdom.render(&mut real_dom)
}
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
# fn app() -> Element { None }
# struct SomeRenderer; impl SomeRenderer { fn new() -> SomeRenderer { SomeRenderer; } async fn event() -> () { unimplemented!() } }
2024-01-30 01:02:26 +00:00
```
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
## Features
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
A virtualdom is an efficient and flexible tree datastructure that allows you to manage state for a graphical user interface. The Dioxus VirtualDom is perhaps the most fully-featured virtualdom implementation in Rust and powers renderers running across Web, Desktop, Mobile, SSR, TUI, LiveView, and more. When you use the Dioxus VirtualDom, you immediately enable users of your renderer to leverage the wide ecosystem of Dioxus components, hooks, and associated tooling.
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
Some features of `dioxus-core` include:
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
- UI components are just functions
- State is provided by hooks
- Deep integration with async
- Strong focus on performance
- Integrated hotreloading support
- Extensible system for UI elements and their attributes
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
If you are just starting, check out the Guides first.
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
## Understanding the implementation
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
`dioxus-core` is designed to be a lightweight crate that. It exposes a number of flexible primitives without being deeply concerned about the intracices of state management itself. We proivde a number of useful abstractions built on these primitives in the `dioxus-hooks` crate as well as the `dioxus-signals` crate.
The important abstractions to understand are:
- The [`VirtualDom`]
- The [`Component`] and its [`Properties`]
- Handling events
- Working with async
- Suspense
## Usage
The `dioxus` crate exports the `rsx` macro which transforms a helpful, simpler syntax of Rust.
2022-11-30 15:31:44 +00:00
First, start with your app:
2021-12-21 05:58:14 +00:00
2023-01-12 01:12:09 +00:00
```rust
2024-01-16 01:14:11 +00:00
# use dioxus::dioxus_core::Mutations;
2023-01-12 01:12:09 +00:00
use dioxus::prelude::*;
// First, declare a root component
fn app() -> Element {
2024-01-14 05:12:21 +00:00
rsx!{
2023-01-12 01:12:09 +00:00
div { "hello world" }
}
2021-12-21 05:58:14 +00:00
}
2023-01-12 01:12:09 +00:00
fn main() {
// Next, create a new VirtualDom using this app as the root component.
let mut dom = VirtualDom::new(app);
2021-12-21 05:58:14 +00:00
2023-01-12 01:12:09 +00:00
// The initial render of the dom will generate a stream of edits for the real dom to apply
let mutations = dom.rebuild_to_vec();
2023-01-12 01:12:09 +00:00
}
2022-11-30 15:31:44 +00:00
```
2021-12-21 06:11:27 +00:00
2024-01-30 01:02:26 +00:00
## Contributing
- Check out the website [section on contributing](https://dioxuslabs.com/learn/0.5/contributing).
2024-01-30 01:02:26 +00:00
- Report issues on our [issue tracker](https://github.com/dioxuslabs/dioxus/issues).
- [Join](https://discord.gg/XgGxMSkvUM) the discord and ask questions!
2023-01-12 01:12:09 +00:00
2024-01-30 01:02:26 +00:00
<a href="https://github.com/dioxuslabs/dioxus/graphs/contributors">
<img src="https://contrib.rocks/image?repo=dioxuslabs/dioxus&max=30&columns=10" />
</a>
2021-01-14 07:56:41 +00:00
2024-01-30 01:02:26 +00:00
## License
This project is licensed under the [MIT license].
2021-01-14 07:56:41 +00:00
2024-01-30 01:02:26 +00:00
[mit license]: https://github.com/DioxusLabs/dioxus/blob/master/LICENSE-MIT
2021-01-14 07:56:41 +00:00
2024-01-30 01:02:26 +00:00
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Dioxus by you, shall be licensed as MIT, without any additional
terms or conditions.