Socket
Book a DemoInstallSign in
Socket

f-box-react

Package Overview
Dependencies
Maintainers
0
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

f-box-react

React hooks and utilities for working with f-box-core.

0.2.6
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
0
Weekly downloads
 
Created
Source

F-Box React

F-Box React provides React hooks and utilities to seamlessly integrate F-Box into your React applications. With useBox, useRBox, useRBoxForm, and useRBoxTransaction, you can manage state reactively and functionally, leveraging the abstractions provided by F-Box.

HookDescription
useBoxA hook for managing static values with the Box abstraction.
useRBoxA hook for managing reactive state with RBox.
useRBoxFormA utility hook for form state management with validation.
useRBoxTransactionA utility hook for handling async state transitions with ease.

Installation

Install via npm:

npm install f-box-react

Note: f-box-react requires f-box-core, react, and react-dom as peerDependencies. Install them if not already available:

npm install f-box-core react react-dom

Usage

useBox

useBox allows you to work with static values encapsulated in a Box, using F-Box's operators like ["<$>"], ["<*>"], and [">>="].

Example

import { useBox } from "f-box-react";

function App() {
  const [value, valueBox] = useBox(10); // Initial value is 10.

  // Derive new values using ["<$>"]
  const [squared] = useBox(() => valueBox["<$>"]((x) => x * x));

  return (
    <div>
      <p>Original Value: {value}</p>
      <p>Squared Value: {squared}</p>
    </div>
  );
}

useRBox

useRBox is the core hook for integrating F-Box's reactive state management (RBox) into React components. It allows seamless connection between reactive state and React's rendering lifecycle.

Example: Local State

import { useRBox, set } from "f-box-react";

function Counter() {
  const [count, countBox] = useRBox(0); // Initialize with 0.
  const setCount = set(countBox);

  const increment = () => setCount(count + 1);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

Example: Global State

import { RBox } from "f-box-core";
import { useRBox, set } from "f-box-react";

const countBox = RBox.pack(0); // Create a global reactive state.

function Counter() {
  const [count] = useRBox(countBox); // Bind global state to local variable.
  const setCount = set(countBox);

  const increment = () => setCount(count + 1);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

function ResetButton() {
  const setCount = set(countBox);

  const reset = () => setCount(0); // Reset state to 0.

  return <button onClick={reset}>Reset</button>;
}

useRBoxForm

useRBoxForm simplifies form state management by leveraging RBox. It provides validation, error handling, and utility functions to streamline form handling.

Example

import { useRBoxForm } from "f-box-react";

type Form = {
  name: string;
  email: string;
  message: string;
};

const initialValues: Form = { name: "", email: "", message: "" };

const validate = (form: Form) => ({
  name: [() => form.name.length >= 3, () => /^[a-zA-Z]+$/.test(form.name)],
  email: [() => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email)],
  message: [() => form.message.length >= 10],
});

function ContactForm() {
  const { form, handleChange, handleValidatedSubmit, renderErrorMessages } =
    useRBoxForm<Form>(initialValues, validate);

  const handleSubmit = handleValidatedSubmit((form) => {
    console.log("Form submitted:", form);
  });

  return (
    <form onSubmit={handleSubmit}>
      <label>
        Name:
        <input
          value={form.name}
          onChange={(e) => handleChange("name", e.target.value)}
        />
        {renderErrorMessages("name", [
          "Name must be at least 3 characters.",
          "Name must only contain letters.",
        ])}
      </label>
      <label>
        Email:
        <input
          value={form.email}
          onChange={(e) => handleChange("email", e.target.value)}
        />
        {renderErrorMessages("email", ["Invalid email format."])}
      </label>
      <label>
        Message:
        <textarea
          value={form.message}
          onChange={(e) => handleChange("message", e.target.value)}
        />
        {renderErrorMessages("message", [
          "Message must be at least 10 characters.",
        ])}
      </label>
      <button type="submit">Submit</button>
    </form>
  );
}

useRBoxTransaction

A hook for managing asynchronous state transitions reactively. It tracks whether a transaction is pending and ensures state updates are encapsulated within the transaction lifecycle.

Example

import { useRBoxTransaction } from "f-box-react";

function AsyncAction() {
  const [isPending, startTransaction] = useRBoxTransaction();

  const performAction = async () => {
    await startTransaction(async () => {
      console.log("Transaction started");
      await new Promise((resolve) => setTimeout(resolve, 2000)); // Simulate async work
      console.log("Transaction finished");
    });
  };

  return (
    <div>
      <p>{isPending ? "Processing..." : "Idle"}</p>
      <button onClick={performAction} disabled={isPending}>
        Perform Async Action
      </button>
    </div>
  );
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Keywords

typescript

FAQs

Package last updated on 22 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.