Skip to content

Commit f347657

Browse files
committed
chore: Update docs
1 parent 7a4710f commit f347657

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

client/src/lib/hooks/usetodo.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@ import { useSyncExternalStore } from 'react'
22

33
// https://react.dev/reference/react/useSyncExternalStore
44

5+
// Global item id
56
let nextId = 0
7+
8+
// Global store containing an array of objects
69
let todos = [{ id: nextId++, text: 'Todo #1' }]
10+
11+
// Internal listeners for each method in todoStore initialized by useSyncExternalStore.
12+
// Needs to call emitChange() to take effect
713
let listeners = []
814

15+
/**
16+
* Exportable hook that uses useSyncExternalStore and a global variable to store data.
17+
* Usage: const { todos, addTodo, deleteTodo } = useTodos()
18+
* @returns {Object} { todos, addTodo, deleteTodo }
19+
*/
920
export default function useTodos () {
1021
const todos = useSyncExternalStore(
1122
todoStore.subscribe,
@@ -20,6 +31,12 @@ export default function useTodos () {
2031
}
2132
}
2233

34+
/**
35+
* The external store with available useSyncExternalStore-required methods:
36+
* subscribe() - Used and initialized internally by useSyncExternalStore
37+
* getSnapshot() - Returns the current (full) snapshot of the global data variable
38+
* getServerSnapshot() - Used in SSR
39+
*/
2340
export const todoStore = {
2441
addTodo () {
2542
todos = [ ...todos, { id: nextId++, text: 'Todo #' + nextId }]

0 commit comments

Comments
 (0)