Code Monkey home page Code Monkey logo

react-wasm-bridge's Introduction

react-wasm-bridge

Build react components with wasm modules (currently only with rust).

Using react-wasm-bridge in your project

Rust-side

Install rust nightly and set up a new rust library. Make sure you set the crate-type to "cdylib" and add both wasm-bindgen and react-wasm-bridge as dependencies.

Your module should export a function called render that takes two parameters: state and factory.

#[wasm_bindgen]
pub fn render(state: &State, factory: &Builder) -> JsValue {
    ...
}

state is an object that holds the component props. You'll use factory to generate elements and return a root elem (details TBD).

Build the module (cargo +nightly build --target wasm32-unknown-unknown) and run it through wasm-bindgen (wasm-bindgen target/wasm32-unknown-unknown/$(MODE)/hello.wasm --out-dir . or similar).

React-side

Add react-wasm-bridge to your react project. Then load your wasm module and pass it to the <ReactWasmBridge/> component as the module prop. Any other props will be passed to the underlying rust module into its render function (strings and numbers only).

Building the library

To build a transpiled version of the library, make sure you have all of these things:

  • yarn
  • rust nightly
  • wasm-bindgen
  • Firefox

and run make. The transpiled version will be output into dist/.

Building the examples

Run make examples and it'll build all of them. Or just go into each example directory to make them individually.

Going faster

By default, the examples build in debug mode. Use make run MODE=release and it'll build in release mode. If you've already built it, you'll have to make clean or it won't rebuild.

react-wasm-bridge's People

Contributors

chip-so avatar mooreoak avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.