Coder Social home page Coder Social logo

extism / react Goto Github PK

View Code? Open in Web Editor NEW
8.0 8.0 0.0 233 KB

Extism goodies for React developers

Home Page: https://www.npmjs.com/package/@extism/react

License: BSD 3-Clause "New" or "Revised" License

JavaScript 29.18% CSS 9.27% TypeScript 44.91% HTML 16.64%

react's Introduction

React Utilities for Extism

This repository contains hooks as well as a React component to make it easy for React developers to integrate Extism into their applications. The implementation of these utilities is based off of the Universal JavaScript SDK. In some cases, you may be better off using that.

Installation

npm install @extism/react

Hooks

usePlugin

The usePlugin hook is the primary export of this library. It allows developers to easily initialize and execute Extism plugins.

import { usePlugin } from '@extism/react'

// ...

function MyFunctionalComponent() {
  const { plugin, loading: pluginLoading, useFunction } = usePlugin(pluginSource, { useWasi: true })
  const input = 'some input'
  useEffect(() => {
    plugin?.call('count_vowels', input).then((output) => {
      // do stuff with output
    })
  }, [plugin, input])
  return (
    // ...
  )
}

Check out the source

Check out the examples for exact usage

useFunction

The useFunction hook is returned from usePlugin and serves as a convenience hook to allow developers to more tersely invoke Extism functions.

import { usePlugin } from '@extism/react'

// ...

function MyFunctionalComponent() {
  const { plugin, loading: pluginLoading, useFunction } = usePlugin(pluginSource, { useWasi: true })
  const input = 'some input'
  const { output, loading } = useFunction('count_vowels', hookInput);
  // do stuff with output
  return (
    // ...
  )
}

Check out the source

Check out the examples for exact usage

Component

ExtismPlugin

The ExtismPlugin component is currently the only React component exposed by this library. It allows developers to initialize Extism plugins using JSX and specify which plugin function should be called. When rendered without any children, the output of the plugin function is decoded as text and rendered as HTML. When rendered with children, the output of the plugin function is passed as an argument to the children to allow the developer more flexibility.

import { ExtismPlugin } from '@extism/react'

function MyComponent() {
  return (
    <div>
      <ExtismPlugin source='https://location_of_extism_plugin.wasm' input={someInput} functionName='some_function_name' />
    </div>
  )
}

Check out the source

Check out the examples for exact usage

Examples

An example application was created using npx create-react-app example and can be found in the root of this repository. The App.js component contains an example usage of the usePlugin hook, and the ExtismPlugin component. Check out the comments in the code for how to use the useFunction hook, and rendering ExtismPlugin with children. You may start the application by running npm start in the example directory (after running npm i)

Questions?

File an issue or reach out to us on Discord!

react's People

Contributors

bhelx avatar nilslice avatar wikiwong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react's Issues

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.