React Haiku Hooks & Utilities

2.4.1 · active · verified Sun Apr 19

React Haiku is a lightweight utility library offering a comprehensive collection of React Hooks and utility components designed to streamline development and enhance efficiency. Currently at version 2.4.1, the library maintains an active release cadence, frequently adding new hooks and improving existing ones, as evidenced by multiple minor releases and updates in recent months. Its primary goal is to provide developers with ready-to-use solutions for common React patterns, such as state management, DOM manipulation, and browser API interactions, thereby saving development time. Key differentiators include its focus on being a "clean & lightweight" collection, providing a wide array of specialized hooks like `useHover`, `useBatteryStatus`, `useCookie`, and utility components like `<For />` and `<Switch />`, all while shipping with TypeScript types for improved developer experience. It requires React version 16.8.0 or newer.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates common usage patterns for `useHover`, `useTitle`, and `useCookie` hooks within a React component, showcasing state management and side effects.

import React from 'react';
import { useHover, useTitle, useCookie } from 'react-haiku';

// Example 1: useHover hook
const HoverComponent = () => {
  const { hovered, ref } = useHover();
  return (
    <div ref={ref} style={{ padding: '20px', border: '1px solid gray', marginBottom: '20px' }}>
      <p>{hovered ? 'Mouse is hovering over me!' : 'Hover over me'}</p>
    </div>
  );
};

// Example 2: useTitle hook
const TitleUpdater = () => {
  const [currentTitle, setTitle] = React.useState('Haiku App');
  useTitle(currentTitle);
  return (
    <button onClick={() => setTitle(`Haiku Title: ${Math.random().toFixed(2)}`)}>
      Change Page Title
    </button>
  );
};

// Example 3: useCookie hook
const CookieManager = () => {
  const [value, updateCookie, deleteCookie] = useCookie('my_haiku_cookie', 'initial_value');

  return (
    <div style={{ marginTop: '20px' }}>
      <p>Cookie "my_haiku_cookie": {value ?? 'Not set'}</p>
      <button onClick={() => updateCookie(`new_value_${Date.now()}`)}>Update Cookie</button>
      <button onClick={deleteCookie} style={{ marginLeft: '10px' }}>Delete Cookie</button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <h1>React Haiku Examples</h1>
      <HoverComponent />
      <TitleUpdater />
      <CookieManager />
    </div>
  );
};

export default App;

view raw JSON →