TodoMVC
This is an example of real-time collaborative TodoMVC using React and Yorkie JS SDK.
App.tsx
1import 'todomvc-app-css/index.css';2import './App.css';34import { JSONArray } from '@yorkie-js/react';5import ExampleHeader, { type Status } from './ExampleHeader';6import Header from './components/Header';7import MainSection from './components/MainSection';8import { Todo } from './model';9import { useTodoReducer } from './useTodoReducer';1011const initialRoot = {12 todos: [13 { id: crypto.randomUUID(), text: 'Yorkie JS SDK', completed: false },14 { id: crypto.randomUUID(), text: 'Garbage collection', completed: false },15 { id: crypto.randomUUID(), text: 'RichText datatype', completed: false },16 ],17} as { todos: JSONArray<Todo> };1819/**20 * `App` is the root component of the application.21 */22export default function App() {23 const { root, dispatch, connection, loading, error } =24 useTodoReducer(initialRoot);2526 let status: Status = 'offline';27 if (error) status = 'error';28 else if (loading) status = 'connecting';29 else if (connection === 'connected') status = 'connected';3031 return (32 <>33 <ExampleHeader title="React TodoMVC" status={status} />34 <main className="page">35 {error ? (36 <p className="state-message" role="alert">37 Couldn't reach the Yorkie server at{' '}38 {import.meta.env.VITE_YORKIE_API_ADDR}. Check that it's running,39 then reload.40 </p>41 ) : loading ? (42 <p className="state-message">Loading the shared todo list…</p>43 ) : (44 <>45 <section className="todoapp">46 <Header dispatch={dispatch} />47 <MainSection todos={root.todos} dispatch={dispatch} />48 </section>49 <footer className="info">50 <p>Double-click to edit a todo</p>51 </footer>52 </>53 )}54 </main>55 </>56 );57}
Two clients on the same document. Edit in one and watch the other.This is one client. Open the example in another tab or on another device to watch edits sync.
- User 1