Skip to content

Commit

Permalink
doc: realtime state added
Browse files Browse the repository at this point in the history
  • Loading branch information
activenode committed Jun 6, 2023
1 parent 44a07cf commit bb4ca68
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion docs/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,42 @@ export const backupTheStateForWhateverReason = () => {
}, 10000);
}
`.trim();
const getRealtimeSample = `
import { useStore } from "./utils/store";
/**
* Assume initial data is:
* todos = ['My first todo']
*/
export default function Todos() {
const [ todos, realtimeTodos ] = useStore(
state => state.todos
);
useEffect(() => {
update.merge({ todos: ['My second todo'] });
}, []);
useEffect(() => {
// your function closure has a todo variable
// with ONE entry ( ["My first todo"] )
// if, for whatever reason, you need the state
// in realtime, so ignoring the current function closure / lifecycle
// you can use
const _todos = realtimeTodos();
// again: normally you don't need this.
// because react will re-render with the new state
}, []);
return (
// ...
);
}
`.trim();
---

<Layout title="unglitch - THE state manager.">
Expand Down Expand Up @@ -539,7 +575,7 @@ export const backupTheStateForWhateverReason = () => {

<ul role="list" class="link-card-grid flex flex-col gap-5">
<Card
title="getSnapshot: Getting the whole state in this exact millisecond"
title="getSnapshot"
lang="typescript"
file="your_app/utils/backup.ts"
>
Expand All @@ -551,6 +587,20 @@ export const backupTheStateForWhateverReason = () => {
other services / libraries.
</div>
</Card>

<Card
title="getRealtime"
lang="typescript"
file="your_app/utils/YourComponent.tsx"
>
{getRealtimeSample}

<div class="card-desc" slot="description">
Also use this one carefully. If you use it too often you might
simply be having an architectural problem. But it's more likely to
need this than <code>getSnapshot</code>
</div>
</Card>
</ul>

<div class="h-32"></div>
Expand Down

1 comment on commit bb4ca68

@vercel
Copy link

@vercel vercel bot commented on bb4ca68 Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.