Skip to content

Commit a8df171

Browse files
committed
docs: add poll state overview docs
1 parent 334f05e commit a8df171

File tree

1 file changed

+70
-4
lines changed

1 file changed

+70
-4
lines changed

docusaurus/docs/reactnative/state-and-offline-support/state-overview.mdx

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ To disconnect a user you can call `disconnect` on the client.
248248
await client.disconnectUser();
249249
```
250250

251-
## Thread and ThreadManager
251+
## POJO
252252

253-
With the new [threads feature](../guides/custom-thread-list.mdx) we've decided to refresh our state management and moved to a subscribable POJO with selector based system to make developer experience better when it came to rendering information provided by our `StreamChat` client.
253+
With a few of our new features, we've decided to refresh our state management and moved to a subscribable POJO with selector based system to make developer experience better when it came to rendering information provided by our `StreamChat` client.
254254

255255
:::note
256-
This change is currently only available within `StreamChat.threads` but will be reused across the whole SDK later on.
256+
This change is currently only available within `StreamChat.threads`, `StreamChat.poll` and `StreamChat.polls` but will be reused across the whole SDK later on.
257257
:::
258258

259259
### Why POJO (State Object)
@@ -349,7 +349,7 @@ thread?.state.getLatestValue(/*...*/);
349349

350350
#### useStateStore Hook
351351

352-
For the ease of use - the React SDK comes with the appropriate state access hook which wraps `StateStore.subscribeWithSelector` API for the React-based applications.
352+
For the ease of use - the React Native SDK comes with the appropriate state access hook which wraps `StateStore.subscribeWithSelector` API for the React-based applications.
353353

354354
```tsx
355355
import { useStateStore } from 'stream-chat-react-native';
@@ -370,3 +370,69 @@ const CustomThreadList = () => {
370370
);
371371
};
372372
```
373+
374+
## Thread and ThreadManager
375+
376+
One of the feature that follows the new POJO style of state management is the [threads feature](../guides/custom-thread-list.mdx).
377+
378+
It provides a reactive state for both a single `Thread` (through a reactive `threadInstance`) and a list of threads (through `StreamChat.threads`).
379+
380+
Both states can be accessed through `selector`s as outlined in the examples above.
381+
382+
## Poll and PollManager
383+
384+
Our new polls feature also follows the new POJO style of state management. A `poll` in itself is something that needs to be linked to a `message` in order for it to work. When a poll is created, the only way to make it visible to the users is to send it as a message. This `message` needs to have a `poll_id` attached to it and preferably no text.
385+
386+
You can access each `poll`'s reactive state by getting it by ID using `StreamChat.polls.fromState(<poll-id>)`.
387+
388+
:::note
389+
Please keep in mind that `message.poll` is not going to be reactive, but will rather contain the raw `poll` data as returned by our backend.
390+
:::
391+
392+
### Utility hooks
393+
394+
The React Native SDK provides 2 utility hooks to help with consuming the `poll` state. They can be found listed below:
395+
396+
- [`usePollStateStore`](./hooks/poll/use-poll-state-store.mdx)
397+
- [`usePollState`](./hooks/poll/use-poll-state.mdx)
398+
399+
Similarly to the `threads` feature, one can also directly use `useStateStore` and access `StreamChat.polls.fromState(<poll-id>).state` through custom `selector`s.
400+
401+
:::note
402+
Both `usePollStateStore` and `usePollState` can only be used in children of a [`PollContext`](./contexts/poll-context.mdx). This impediment does not exist however on `useStateStore`.
403+
:::
404+
405+
Due to this, all `poll` related components within the SDK are self-wrapped within a `PollContext` and require `message` and `poll` as mandatory props.
406+
407+
#### Example
408+
409+
```tsx
410+
import { usePollState } from 'stream-chat-react-native';
411+
412+
const CustomPollComponent = () => {
413+
const { name, options } = usePollState();
414+
415+
return (
416+
<View>
417+
<Text>{name}</Text>
418+
{options.map(option => (
419+
<Text key={option.id}>{option.text}</Text>
420+
))}
421+
</View>
422+
);
423+
};
424+
425+
const PollMessage = ({ message }) => {
426+
const { client } = useChatContext();
427+
const pollInstance = client.polls.fromState(message?.poll_id);
428+
return (
429+
<PollContextProvider value={{ message, poll: pollInstance }}>
430+
<CustomPollComponent />
431+
</PollContextProvider>
432+
)
433+
}
434+
```
435+
436+
437+
438+

0 commit comments

Comments
 (0)