dioxus/README.md

168 lines
7.5 KiB
Markdown
Raw Normal View History

2021-01-16 15:31:17 +00:00
<div align="center">
<h1>🌗🚀 Dioxus</h1>
<p>
<strong>A concurrent, functional, virtual DOM for Rust</strong>
</p>
</div>
# About
2021-01-14 07:56:41 +00:00
Dioxus is a portable, performant, and ergonomic framework for building cross-platform user experiences in Rust.
```rust
static Example: FC<()> = |ctx| {
let (val1, set_val1) = use_state(&ctx, || "___?");
2021-01-29 16:57:52 +00:00
ctx.view(html! {
<div>
2021-02-08 22:05:58 +00:00
<button onclick={move |_| set_val1("world!")}> "world" </button>
<button onclick={move |_| set_val1("dioxus 🎉")}> "dioxus" </button>
<div>
<p> "Hello, {val1}" </p>
</div>
</div>
})
};
```
The primary Dioxus crate is agnostic to platform and is meant to be configured with external renderers for getting content to the screen. We have built renderers for Dioxus to serve WebApps, Desktop Apps, static pages, liveview, Android, and iOS.
Dioxus is supported by Dioxus Labs, a company providing end-to-end services for building, testing, deploying, and managing Dioxus apps on all supported platforms, designed especially for your next startup.
2021-02-08 22:05:58 +00:00
# Get Started with...
<table style="width:100%">
<tr>
2021-02-08 22:07:27 +00:00
<th>[WebApps](http://github.com/jkelleyrtp/dioxus)</th>
<th>[Desktop](http://github.com/jkelleyrtp/dioxus)</th>
<th>[Mobile](http://github.com/jkelleyrtp/dioxus)</th>
<th>[State Management](http://github.com/jkelleyrtp/dioxus)</th>
<th>[Docs](http://github.com/jkelleyrtp/dioxus)</th>
<th>[Tools](http://github.com/jkelleyrtp/dioxus)</th>
2021-02-08 22:05:58 +00:00
<tr>
</table>
2021-02-08 22:07:27 +00:00
2021-02-08 22:05:58 +00:00
## Features
2021-01-21 07:25:44 +00:00
Dioxus' goal is to be the most advanced UI system for Rust, targeting isomorphism and hybrid approaches. Our goal is to eliminate context-switching for cross-platform development - both in UI patterns and programming language. Hooks and components should work *everywhere* without compromise.
Dioxus Core supports:
2021-02-08 16:12:02 +00:00
- [x] Hooks for component state
- [ ] Concurrent rendering
- [ ] Context subscriptions
- [ ] State management integrations
2021-01-14 07:56:41 +00:00
2021-02-08 16:12:02 +00:00
Separately, we maintain a collection of high quality, cross-platform hooks and services in the dioxus-hooks repo:
- [ ] `dioxus-router`: A hook-based router implementation for Dioxus web apps
We also maintain two state management options that integrate cleanly with Dioxus apps:
- [ ] `dioxus-reducer`: ReduxJs-style global state management
- [ ] `dioxus-dataflow`: RecoilJs-style global state management
2021-01-29 16:57:52 +00:00
## Components
2021-01-15 01:56:28 +00:00
Dioxus should look and feel just like writing functional React components. In Dioxus, there are no class components with lifecycles. All state management is done via hooks. This encourages logic reusability and lessens the burden on Dioxus to maintain a non-breaking lifecycle API.
2021-01-14 07:56:41 +00:00
```rust
#[derive(Properties, PartialEq)]
struct MyProps {
name: String
}
2021-02-08 16:12:02 +00:00
fn Example(ctx: Context<MyProps>) -> VNode {
html! { <div> "Hello {ctx.props.name}!" </div> }
2021-01-14 07:56:41 +00:00
}
```
2021-01-21 07:25:44 +00:00
Here, the `Context` object is used to access hook state, create subscriptions, and interact with the built-in context API. Props, children, and component APIs are accessible via the `Context` object. The functional component macro makes life more productive by inlining props directly as function arguments, similar to how Rocket parses URIs.
2021-01-14 07:56:41 +00:00
2021-01-15 01:56:28 +00:00
```rust
// A very terse component!
2021-01-21 07:25:44 +00:00
#[fc]
2021-02-08 16:12:02 +00:00
fn Example(ctx: Context, name: String) -> VNode {
html! { <div> "Hello {name}!" </div> }
2021-01-15 01:56:28 +00:00
}
2021-01-14 07:56:41 +00:00
2021-01-15 01:56:28 +00:00
// or
2021-01-14 07:56:41 +00:00
2021-01-15 01:56:28 +00:00
#[functional_component]
2021-02-03 07:26:04 +00:00
static Example: FC = |ctx, name: String| html! { <div> "Hello {name}!" </div> };
2021-01-15 01:56:28 +00:00
```
2021-01-14 07:56:41 +00:00
2021-01-22 20:50:16 +00:00
The final output of components must be a tree of VNodes. We provide an html macro for using JSX-style syntax to write these, though, you could use any macro, DSL, templating engine, or the constructors directly.
2021-01-14 07:56:41 +00:00
2021-01-15 01:56:28 +00:00
## Concurrency
2021-02-03 07:26:04 +00:00
In Dioxus, VNodes are asynchronous and can their rendering can be paused at any time by awaiting a future. Hooks can combine this functionality with the Context and Subscription APIs to craft dynamic and efficient user experiences.
2021-01-15 01:56:28 +00:00
```rust
2021-02-08 16:12:02 +00:00
fn user_data(ctx: Context<()>) -> VNode {
2021-02-03 07:26:04 +00:00
// Register this future as a task
use_suspense(ctx, async {
// Continue on with the component as usual, waiting for data to arrive
let Profile { name, birthday, .. } = fetch_data().await;
html! {
<div>
{"Hello, {name}!"}
{if birthday === std::Instant::now() {html! {"Happy birthday!"}}}
</div>
}
})
2021-01-15 01:56:28 +00:00
}
```
Asynchronous components are powerful but can also be easy to misuse as they pause rendering for the component and its children. Refer to the concurrent guide for information on how to best use async components.
2021-01-29 16:57:52 +00:00
## Liveview
With the Context, Subscription, and Asynchronous APIs, we've built Dioxus Liveview: a coupling of frontend and backend to deliver user experiences that do not require dedicated API development. Instead of building and maintaining frontend-specific API endpoints, components can directly access databases, server caches, and other services directly from the component.
These set of features are still experimental. Currently, we're still working on making these components more ergonomic
```rust
2021-02-08 16:12:02 +00:00
fn live_component(ctx: &Context<()>) -> VNode {
2021-01-29 16:57:52 +00:00
use_live_component(
ctx,
// Rendered via the client
#[cfg(target_arch = "wasm32")]
|| html! { <div> {"Loading data from server..."} </div> },
// Renderered on the server
#[cfg(not(target_arch = "wasm32"))]
|| html! { <div> {"Server Data Loaded!"} </div> },
)
}
```
## Dioxus LiveHost
Dioxus LiveHost is a paid service dedicated to hosting your Dioxus Apps - whether they be server-rendered, wasm-only, or a liveview. LiveHost enables a wide set of features:
2021-02-08 16:12:02 +00:00
- Versioned combined frontend and backend with unique access links
- Builtin CI/CD for all supported Dioxus platforms (macOS, Windows, Android, iOS, server, WASM, etc)
- Managed and pluggable storage database backends (PostgresSQL, Redis)
2021-01-29 16:57:52 +00:00
- Serverless support for minimal latency
- Analytics
- Lighthouse optimization
- On-premise support (see license terms)
2021-02-03 07:26:04 +00:00
- Cloudfare/DDoS protection integrations
2021-02-08 16:12:02 +00:00
- Web-based simulators for iOS, Android, Desktop
- Team + company management
2021-01-29 16:57:52 +00:00
2021-02-08 16:12:02 +00:00
For small teams, LiveHost is free 🎉. Check out the pricing page to see if Dioxus LiveHost is good fit for your team.
2021-01-29 16:57:52 +00:00
2021-01-16 04:25:29 +00:00
## Examples
2021-01-21 07:25:44 +00:00
We use the dedicated `dioxus-cli` to build and test dioxus web-apps. This can run examples, tests, build web workers, launch development servers, bundle, and more. It's general purpose, but currently very tailored to Dioxus for liveview and bundling. If you've not used it before, `cargo install --path pacakages/dioxus-cli` will get it installed. This CLI tool should feel like using `cargo` but with 1st party support for assets, bundling, and other important dioxus-specific features.
2021-01-16 04:25:29 +00:00
Alternatively, `trunk` works but can't run examples.
2021-01-16 04:32:53 +00:00
- tide_ssr: Handle an HTTP request and return an html body using the html! macro. `cargo run --example tide_ssr`
2021-02-03 07:26:04 +00:00
- doc_generator: Use dioxus SSR to generate the website and docs. `cargo run --example doc_generator`
2021-01-22 20:50:16 +00:00
- fc_macro: Use the functional component macro to build terse components. `cargo run --example fc_macro`
2021-02-03 07:26:04 +00:00
- hello_web: Start a simple wasm app. Requires a web packer like dioxus-cli or trunk `cargo run --example hello`
2021-01-22 20:50:16 +00:00
- router: `cargo run --example router`
- tide_ssr: `cargo run --example tide_ssr`
- webview: Use liveview to bridge into a webview context for a simple desktop application. `cargo run --example webview`
- twitter-clone: A full-featured Twitter clone showcasing dioxus-liveview, state management patterns, and hooks. `cargo run --example twitter`
2021-01-15 01:56:28 +00:00
## Documentation
2021-01-29 16:57:52 +00:00
2021-01-14 07:56:41 +00:00