Skip to content

Commit 3406984

Browse files
authored
chore: update create-react-app example in Readme (#59)
I tried following the example and ran into some issues. Here, I have updated the Readme to fix these. - add correct imports - fix syntax - add reference to more information on specifying a web assembly module - convert the example to use `onlinePlayground` which is preferred - updates React syntax - turns the example into a basic tasks app
1 parent b7bc58b commit 3406984

File tree

1 file changed

+63
-38
lines changed

1 file changed

+63
-38
lines changed

README.md

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -187,46 +187,63 @@ yarn add @dittolive/ditto @dittolive/react-ditto
187187
2. In `./src/index.js`, or `./src/index.tsx` if you're using typescript, setup Ditto with the `DittoProvider` like so:
188188

189189
```tsx
190-
import { DittoProvider, useOfflinePlaygroundIdentity } from '@dittolive/react-ditto'
190+
// ... other imports from create-react-app above
191+
import { Ditto } from "@dittolive/ditto";
192+
import {
193+
DittoProvider,
194+
useOnlinePlaygroundIdentity,
195+
} from "@dittolive/react-ditto";
191196

192197
/**
193-
* This configuration is optional for web browser-based react applications.
194-
* This tells the `DittoProvider` where it should load the .wasm file. If no path is provided (ie. initOptions is undefined),
195-
* the wasm will be loaded from our CDN.
198+
* This configuration is optional for web browser-based react applications. This
199+
* tells the `DittoProvider` where it should load the .wasm file. If no path is
200+
* provided (ie. initOptions is undefined), the wasm will be loaded from our
201+
* CDN. If you enable this, make sure to serve the `ditto.wasm` file with the
202+
* correct MIME type and CORS headers. See
203+
* https://www.npmjs.com/package/@dittolive/ditto#browser-environments for
204+
* details.
196205
**/
197206
const initOptions = {
198-
webAssemblyModule: '/ditto.wasm',
199-
}
207+
webAssemblyModule: "/ditto.wasm",
208+
};
200209

201210
/** Example of a React root component setting up a single ditto instance that uses a development connection */
202211
const RootComponent = () => {
203-
const { create } = useOfflinePlaygroundIdentity()
212+
const { create } = useOnlinePlaygroundIdentity();
204213

205214
return (
206215
<DittoProvider
207-
setup={() => {
208-
const ditto = new Ditto(create({ appName: 'my app', siteID: 1234 }, '/foo'))
209-
ditto.startSync()
210-
return ditto
216+
setup={async () => {
217+
const ditto = new Ditto(
218+
create({
219+
// Create an app on https://portal.ditto.live/ and follow the
220+
// instructions to get your token. Replace the placeholders below
221+
// with your app id and token.
222+
appID: "your-app-id",
223+
token: "your-online-playground-token",
224+
}),
225+
"testing"
226+
);
227+
await ditto.disableSyncWithV3();
228+
ditto.startSync();
229+
return ditto;
211230
}}
212-
/*initOptions={initOptions} */
231+
/* initOptions={initOptions} */
213232
>
214233
{({ loading, error }) => {
215234
if (loading) return <p>Loading</p>;
216235
if (error) return <p>{error.message}</p>;
217236
return <App />;
218237
}}
219238
</DittoProvider>
220-
)
221-
}
222-
239+
);
240+
};
223241

224-
225-
ReactDOM.render(
242+
const root = ReactDOM.createRoot(document.getElementById("root"));
243+
root.render(
226244
<React.StrictMode>
227245
<RootComponent />
228-
</React.StrictMode>,
229-
document.getElementById('root')
246+
</React.StrictMode>
230247
);
231248
```
232249

@@ -236,45 +253,53 @@ ReactDOM.render(
236253
import { usePendingCursorOperation, useMutations } from '@dittolive/react-ditto';
237254

238255
export default function App() {
239-
const { documents, ditto } = usePendingCursorOperation({
256+
const { documents } = usePendingCursorOperation({
240257
collection: 'tasks',
241258
});
242259

243-
const { updateByID, upsert } = useMutations({ collection: 'tasks' })
260+
const { removeByID, upsert } = useMutations({ collection: 'tasks' })
244261

245262
return (
246-
<ul>
247-
{documents.map(doc => (
248-
<li key={doc._id}>
249-
{doc.body}
250-
</li>
251-
))}
252-
</ul>
263+
<>
264+
<button onClick={() => upsert({ value: { text: 'Hello' } })}>
265+
Add Task
266+
</button>
267+
<ul>
268+
{documents.map((doc) => (
269+
<li key={doc._id}>
270+
{JSON.stringify(doc.value)}
271+
<button onClick={() => removeByID({ _id: doc.id })}>remove</button>
272+
</li>
273+
))}
274+
</ul>
275+
</>
253276
)
254277
}
255278
```
256279

257280
Alternatively, you can also choose to go with the lazy variants of these hooks (`useLazyPendingCursorOperation` and `useLazyPendingIDSpecificOperation`), in order to launch queries on the data store as a response to a user event:
258281

259282
```tsx
260-
import { usePendingCursorOperation, useMutations } from '@dittolive/react-ditto';
283+
import { useLazyPendingCursorOperation } from '@dittolive/react-ditto';
261284

262285
export default function App() {
263-
const { documents, ditto, exec } = useLazyPendingCursorOperation();
264-
265-
if(!documents?.length) {
266-
return <button onClick={() => exec({ collection: 'tasks' })}>Click to load!</button>
286+
const { documents, exec } = useLazyPendingCursorOperation();
287+
288+
if (!documents?.length) {
289+
return (
290+
<button onClick={() => exec({ collection: "tasks" })}>
291+
Click to load!
292+
</button>
293+
);
267294
}
268295

269296
return (
270297
<ul>
271-
{documents.map(doc => (
272-
<li key={doc._id}>
273-
{doc.body}
274-
</li>
298+
{documents.map((doc) => (
299+
<li key={doc._id}>{JSON.stringify(doc.value)}</li>
275300
))}
276301
</ul>
277-
)
302+
);
278303
}
279304
```
280305

0 commit comments

Comments
 (0)