# cero-base/react

React hooks and providers for cero-base. Works with both direct Cero instances and RPC proxy clients.

## Setup

```jsx
import { CeroProvider } from 'cero-base/react'

const db = new Chats('./data')

createRoot(root).render(
  <CeroProvider value={db}>
    <App />
  </CeroProvider>
)
```

## Hooks

### useCero()

Access the Cero instance from context.

```jsx
const db = useCero()
```

### useRoom()

Access the current Room from context. Returns `null` if no `RoomProvider`.

```jsx
const room = useRoom()
```

### useCollection(name, query?)

Subscribe to a collection. Returns live data + CRUD methods.

- Local/private collections: uses `useCero()` context
- Shared collections: requires `RoomProvider`

```jsx
const { data, loading, insert, update, remove, get } = useCollection('tasks')

// With filter
const { data: done } = useCollection('tasks', { done: true })
```

### useRooms()

List all rooms. Re-fetches on 'update' events.

```jsx
const { data: rooms, loading } = useRooms()
```

### useMembers()

Subscribe to room members. Requires `RoomProvider`.

```jsx
const { data: members, loading } = useMembers()
```

### useProfile()

Subscribe to identity profile. Returns profile data + setter.

```jsx
const { data: profile, loading, set } = useProfile()
await set({ name: 'Alice' })
```

## Room Context

Wrap shared collection views with `RoomProvider`:

```jsx
import { RoomProvider } from 'cero-base/react'
import { Room } from 'cero-base'

const room = await Room.from(db, roomId)

<RoomProvider value={room}>
  <ChatView />  {/* useCollection('messages') works here */}
</RoomProvider>
```
