0.2.4 • Published 4 months ago

@tmelliott/react-rserve v0.2.4

Weekly downloads
-
License
-
Repository
github
Last release
4 months ago

react-rserve

R-CMD-check NPM Version

A React library that provides utilities for connecting to and using Rserve.

Development

Changesets

This project uses changesets for version management and releases.

To create a new changeset:

pnpm changeset

This will prompt you to select the type of change (patch, minor, major) and to provide a description of the change.

The repository is configured with GitHub Actions that will automatically create a Release PR when changesets are pushed to the main branch.

Example usage

  1. Create Rserve context provider:
// utils/rserve.ts
import { createRserveProvider } from "react-rserve";

// generated using the `ts` R package: github.com/tmelliott/ts
import app from "../path/to/app.ts";

export const { RserveContext, RserveProvider, useRserve } =
  createRserveProvider(app, {
    host: "localhost:6311",
  });

TODO: create this file with the ts package.

  1. Set up the provider:
// App.tsx
import { RserveProvider } from "./utils/rserve";
import DemoComponent from "./DemoComponent";

export default function App() {
  return (
    <RserveProvider>
      <div>
        <h1>React Rserve</h1>
        <DemoComponent />
      </div>
    </RserveProvider>
  );
}
  1. Use the app in components:
// DemoComponent.tsx
import { rserve } from "./utils/rserve";

export default function DemoComponent() {
  const { app } = useRserve();
  const { result, loading, error } = useOcap(app?.fun);

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error) {
    return <p>Error: {error}</p>;
  }

  return <p>{result}</p>; // "Hello, world!"
}

If an Ocap returns more Ocaps, you need to use conditional components.

import type { App } from "../path/to/app.ts";

// AnotherComponent.tsx
function AnotherComponent() {
  const { app } = useRserve();
  const { result, loading } = useOcap(app?.anotherFun);

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error !== undefined) {
    return <p>Error: {error}</p>;
  }

  return <SubComponent ocap={result} />;
}

type AnotherFunResult = Awaited<ReturnType<App.anotherFun>>;

function SubComponent({ features }: { features: AnotherFunResult }) {
  const { result, loading, error } = useOcap(features.secondaryFunction);

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error !== undefined) {
    return <p>Error: {error}</p>;
  }

  return <p>{result}</p>;
}
0.2.3

4 months ago

0.2.4

4 months ago

0.2.0

4 months ago

0.2.2

4 months ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago