wasm_react/
lib.rs

1#![warn(missing_docs)]
2#![doc = include_str!("../README.md")]
3
4// This hack is needed to let the doctests run for our README file
5#[cfg(doctest)]
6#[doc = include_str!("../README.md")]
7extern "C" {}
8
9mod builtin_components;
10mod callback;
11mod component;
12mod context;
13mod macros;
14mod prop_container;
15mod vnode;
16
17pub mod hooks;
18pub mod props;
19#[doc(hidden)]
20pub mod react_bindings;
21
22use props::Props;
23use wasm_bindgen::prelude::*;
24
25pub use builtin_components::*;
26pub use callback::*;
27pub use component::*;
28pub use context::*;
29#[doc(hidden)]
30pub use paste::paste;
31pub use prop_container::*;
32pub use vnode::*;
33
34/// Contains all functions exported to JS by `wasm-react`. These functions should
35/// be called from JS only.
36#[doc(hidden)]
37#[wasm_bindgen]
38pub struct WasmReact;
39
40#[wasm_bindgen]
41impl WasmReact {
42  /// Set the React runtime that `wasm-react` should use.
43  ///
44  /// Calling this function multiple times will result in no-ops.
45  ///
46  /// # Example
47  ///
48  /// ```js
49  /// import React from "react";
50  /// import init, { WasmReact } from "./path/to/pkg/project.js";
51  ///
52  /// async function main() {
53  ///   await init();
54  ///   WasmReact.useReact(React);
55  /// }
56  ///
57  /// main();
58  /// ```
59  #[wasm_bindgen(js_name = useReact)]
60  pub fn use_react(value: &JsValue) {
61    react_bindings::use_react(value);
62  }
63}
64
65/// The Rust equivalent to `React.createElement`. Use [`h!`] for a more
66/// convenient way to create HTML element nodes. To create Rust components, use
67/// [`Component::build()`].
68pub fn create_element(typ: &JsValue, props: &Props, children: VNode) -> VNode {
69  VNode::Single(react_bindings::create_element(
70    typ,
71    props.as_ref(),
72    &children.into(),
73  ))
74}