From 7c98a1256e0c5cea6f014f0331194b7dd27d7477 Mon Sep 17 00:00:00 2001 From: Youngteac Hong Date: Thu, 6 Mar 2025 11:51:55 +0900 Subject: [PATCH] Add YorkieProvider, DocumentProvider and suspense hooks (#946) YorkieProvider and DocumentProvider are React context providers that enable easy integration of Yorkie clients and documents into React applications. They also provide convenient hooks for accessing and updating shared documents in real-time. ```tsx // ... export default function App() { const { root, update, loading, error } = useDocument(); if (loading) return
Loading...
; if (error) return
Error: {error.message}
; // ... } ``` --- examples/react-todomvc/src/App.tsx | 31 +--- examples/react-todomvc/src/MainSection.tsx | 23 ++- examples/react-todomvc/src/main.tsx | 19 +- packages/react/README.md | 19 ++ packages/react/package.json | 6 +- packages/react/src/DocumentProvider.tsx | 164 ++++++++++++++++++ packages/react/src/YorkieProvider.tsx | 72 ++++++++ packages/react/src/index.ts | 19 +- .../src/{useDocument.ts => useYorkieDoc.ts} | 4 +- packages/react/tsconfig.json | 1 + packages/react/vite.config.js | 2 + packages/sdk/README.md | 21 +++ packages/sdk/package.json | 3 +- packages/sdk/src/yorkie.ts | 1 + 14 files changed, 340 insertions(+), 45 deletions(-) create mode 100644 packages/react/README.md create mode 100644 packages/react/src/DocumentProvider.tsx create mode 100644 packages/react/src/YorkieProvider.tsx rename packages/react/src/{useDocument.ts => useYorkieDoc.ts} (95%) create mode 100644 packages/sdk/README.md diff --git a/examples/react-todomvc/src/App.tsx b/examples/react-todomvc/src/App.tsx index 44a301a1e..7d0caccaa 100644 --- a/examples/react-todomvc/src/App.tsx +++ b/examples/react-todomvc/src/App.tsx @@ -1,46 +1,29 @@ -import { useDocument, JSONArray, JSONObject } from '@yorkie-js/react'; -import 'todomvc-app-css/index.css'; +import { JSONArray, JSONObject, useDocument } from '@yorkie-js/react'; import Header from './Header'; import MainSection from './MainSection'; import { Todo } from './model'; import './App.css'; +import 'todomvc-app-css/index.css'; + /** * `App` is the root component of the application. */ export default function App() { const { root, update, loading, error } = useDocument<{ todos: JSONArray; - }>( - import.meta.env.VITE_YORKIE_API_KEY, - `react-todomvc-${new Date().toISOString().substring(0, 10).replace(/-/g, '')}`, - { - todos: [ - { id: 0, text: 'Yorkie JS SDK', completed: false }, - { id: 1, text: 'Garbage collection', completed: false }, - { id: 2, text: 'RichText datatype', completed: false }, - ], - }, - { - rpcAddr: import.meta.env.VITE_YORKIE_API_ADDR, - }, - ); - - if (loading) { - return
Loading...
; - } + }>(); - if (error) { - return
Error: {error.message}
; - } + if (loading) return
Loading...
; + if (error) return
Error: {error.message}
; const actions = { addTodo: (text: string) => { update((root) => { root.todos.push({ id: - root.todos.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + + root.todos.reduce((maxID, todo) => Math.max(todo.id, maxID), -1) + 1, completed: false, text, diff --git a/examples/react-todomvc/src/MainSection.tsx b/examples/react-todomvc/src/MainSection.tsx index 2e503cc54..632be24fa 100644 --- a/examples/react-todomvc/src/MainSection.tsx +++ b/examples/react-todomvc/src/MainSection.tsx @@ -16,9 +16,8 @@ interface MainSectionProps { actions: { [name: string]: Function }; } -export default function MainSection(props: MainSectionProps) { +export default function MainSection({ todos, actions }: MainSectionProps) { const [filter, setFilter] = useState('SHOW_ALL'); - const { todos, actions } = props; const filteredTodos = todos.filter(TODO_FILTERS[filter]); const completedCount = todos.reduce((count, todo) => { return todo.completed ? count + 1 : count; @@ -37,17 +36,15 @@ export default function MainSection(props: MainSectionProps) { onChange={actions.completeAll as ChangeEventHandler} />