React observing
Advanced to do
Advanced to do
Here is an example of how to use react observing to build a advanced to do.
1import { IObservable, observe, selector, transform, useObserver, useObserverValue } from 'react-observing'23// To do store4const todosStore: IObservable<IObservable<string>[]> = observe([5 observe(''),6]);78// Store the length of to dos based on "todosStore"9const todosLengthStore = transform(todosStore, todos => todos.length);1011// Store the count of the length of all to dos based on "todosStore" and each to do12const todosWordLengthStore = selector({13 get: ({ get }) => {14 const items = get(todosStore);1516 const length = items.reduce((count, todo) => {17 return count += get(todo).length;18 }, 0);1920 return length;21 }22});232425// This component will re-render only if a new to do is added26const ItemsCount = () => {27 const todosLength = useObserverValue(todosLengthStore);28 return <p>(transform) Items count: {todosLength}</p>29}3031// This component will re-render if a new to do is added and if any to do text if changed32const WordCount = () => {33 const todosWordLength = useObserverValue(todosWordLengthStore);34 return <p>(selector) Words count in all items: {todosWordLength}</p>35}3637// This component will be repeated for each to do, and will re-render only if your to do is changed38const Input: React.FC<{ todoObservable: IObservable<string> }> = ({ todoObservable }) => {39 const [todo, setTodo] = useObserver(todoObservable);40 return <input value={todo} onChange={e => setTodo(e.target.value)} />41}4243const TodoItem: React.FC<{ todoObservable: IObservable<string>, onRemove: () => void }> = ({ todoObservable, onRemove }) => {44 return <li>45 <ItemsCount />46 <WordCount />47 <button onClick={onRemove}>Remove</button>48 <Input todoObservable={todoObservable} />49 </li>50}515253export const TodoList = () => {54 const [todos, setTodos] = useObserver(todosStore);5556 return (57 <div>58 <h1>Todo list</h1>5960 <ItemsCount />61 <WordCount />6263 <button onClick={() => setTodos(old => [...old, observe('')])}>Add item</button>6465 <ul>66 {todos.map((todo, index) => (67 <TodoItem68 key={todo.id}69 todoObservable={todo}70 onRemove={() => setTodos(old => [...old.filter((_, i) => i !== index)])}71 />72 ))}73 </ul>74 </div>75 )76}