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-06-07 01:15:17 +00:00
```rust, no_run
# tokio::runtime::Runtime::new().unwrap().block_on(async {
2024-01-30 01:02:26 +00:00
use dioxus_core::prelude::*;
2024-06-07 01:15:17 +00:00
use dioxus_core::*;
2021-03-12 21:58:30 +00:00
2024-06-07 01:15:17 +00:00
let mut vdom = VirtualDom::new(app);
2024-01-30 01:02:26 +00:00
let real_dom = SomeRenderer::new();
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
loop {
2024-06-07 01:15:17 +00:00
tokio::select! {
evt = real_dom.event() => vdom.handle_event("onclick", evt, ElementId(0), true),
2024-01-30 01:02:26 +00:00
_ = vdom.wait_for_work() => {}
}
2024-06-07 01:15:17 +00:00
vdom.render_immediate(& mut real_dom.apply())
2024-01-30 01:02:26 +00:00
}
2022-11-30 15:31:44 +00:00
2024-01-30 01:02:26 +00:00
# fn app() -> Element { None }
2024-06-07 01:15:17 +00:00
# struct SomeRenderer; impl SomeRenderer { fn new() -> SomeRenderer { SomeRenderer } async fn event(&self) -> std::rc::Rc<dyn std::any::Any> { unimplemented!() } fn apply(&self) -> Mutations { Mutations::default() } }
# });
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-06-07 01:15:17 +00:00
A virtualdom is an efficient and flexible tree data structure 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:
2024-06-07 01:15:17 +00:00
2024-01-30 01:02:26 +00:00
- 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
2024-01-11 16:48:04 +00:00
fn app() -> Element {
2024-01-14 05:12:21 +00:00
rsx!{
2023-01-12 01:12:09 +00:00
div { "hello world" }
2024-01-28 09:30:33 +00:00
}
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
2024-01-28 09:30:33 +00:00
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
2024-06-07 01:15:17 +00:00
2024-03-27 02:17:02 +00:00
- 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-06-21 05:35:12 +00:00
2024-01-30 01:02:26 +00:00
## License
2024-06-07 01:15:17 +00:00
2024-01-30 01:02:26 +00:00
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.