npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@better-state/react

v0.2.0

Published

React hooks for Better-State — real-time synced state with one line of code

Readme

@better-state/react

React hooks for Better-State. Real-time synced state with one line of code.

Installation

npm install @better-state/client @better-state/react
# or
pnpm add @better-state/client @better-state/react

Requires React 18+ and a running @better-state/server.

Quick Start

import { BetterStateProvider, useBetterState } from '@better-state/react'

function App() {
  return (
    <BetterStateProvider
      url="http://localhost:3001"
      options={{ apiKey: 'your-api-key' }}
    >
      <Counter />
    </BetterStateProvider>
  )
}

function Counter() {
  const [count, setCount, updateCount] = useBetterState('counter', 0)

  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => updateCount(n => n + 1)}>+1</button>
      <button onClick={() => updateCount(n => n - 1)}>-1</button>
      <button onClick={() => setCount(0)}>Reset</button>
    </div>
  )
}

Open the same page in two tabs. The counter syncs in real-time.

API

<BetterStateProvider>

Wraps your app and creates a single Better-State client for the component tree.

<BetterStateProvider
  url="http://localhost:3001"
  options={{
    apiKey: string,       // required
    namespace?: string,   // default: 'default'
    debug?: boolean,      // default: false
  }}
>
  {children}
</BetterStateProvider>

Place it once at the root of your app (or per-feature if you need multiple servers).

useBetterState(key, initialValue)

Synced state hook. Works like useState, but the value is shared across all connected clients.

const [value, set, update] = useBetterState('my-key', initialValue)

| Return | Type | Description | |--------|------|-------------| | value | T | Current value (reactive) | | set | (value: T) => void | Replace the value | | update | (fn: (prev: T) => T) => void | Transform the value |

Updates are optimistic — the UI updates instantly, then syncs to the server in the background.

function TodoList() {
  const [todos, setTodos, updateTodos] = useBetterState('todos', [])

  const addTodo = (text: string) => {
    updateTodos(list => [...list, { id: crypto.randomUUID(), text, done: false }])
  }

  const toggleTodo = (id: string) => {
    updateTodos(list =>
      list.map(t => t.id === id ? { ...t, done: !t.done } : t)
    )
  }

  return (
    <ul>
      {todos.map(t => (
        <li key={t.id} onClick={() => toggleTodo(t.id)}>
          {t.done ? '✓' : '○'} {t.text}
        </li>
      ))}
    </ul>
  )
}

useConnectionStatus()

Returns the current connection status. Re-renders when it changes.

import { useConnectionStatus } from '@better-state/react'

function StatusIndicator() {
  const status = useConnectionStatus()

  return (
    <span>
      {status === 'connected' && 'Online'}
      {status === 'connecting' && 'Connecting...'}
      {status === 'disconnected' && 'Offline'}
    </span>
  )
}

Returns: 'connecting' | 'connected' | 'disconnected'

useBetterStateClient()

Returns the underlying BetterStateClient instance for advanced use cases (disconnect, error handling, conflict listeners).

import { useBetterStateClient } from '@better-state/react'

function Settings() {
  const client = useBetterStateClient()

  useEffect(() => {
    const unsub = client.onError(err => {
      console.error(err.code, err.message)
    })
    return unsub
  }, [client])

  return <button onClick={() => client.disconnect()}>Disconnect</button>
}

Full Example

import { BetterStateProvider, useBetterState, useConnectionStatus } from '@better-state/react'

function App() {
  return (
    <BetterStateProvider
      url="http://localhost:3001"
      options={{ apiKey: 'your-api-key' }}
    >
      <StatusBar />
      <Counter />
    </BetterStateProvider>
  )
}

function StatusBar() {
  const status = useConnectionStatus()
  return <div>{status === 'connected' ? 'Online' : 'Offline'}</div>
}

function Counter() {
  const [count, , updateCount] = useBetterState('counter', 0)
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => updateCount(n => n + 1)}>+1</button>
    </div>
  )
}

export default App

Peer Dependencies

  • react >= 18.0.0
  • @better-state/client >= 0.1.0

License

MIT