@@ -187,46 +187,63 @@ yarn add @dittolive/ditto @dittolive/react-ditto
187
187
2 . In ` ./src/index.js ` , or ` ./src/index.tsx ` if you're using typescript, setup Ditto with the ` DittoProvider ` like so:
188
188
189
189
``` 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" ;
191
196
192
197
/**
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.
196
205
**/
197
206
const initOptions = {
198
- webAssemblyModule: ' /ditto.wasm' ,
199
- }
207
+ webAssemblyModule: " /ditto.wasm" ,
208
+ };
200
209
201
210
/** Example of a React root component setting up a single ditto instance that uses a development connection */
202
211
const RootComponent = () => {
203
- const { create } = useOfflinePlaygroundIdentity ()
212
+ const { create } = useOnlinePlaygroundIdentity ();
204
213
205
214
return (
206
215
<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 ;
211
230
}}
212
- /* initOptions={initOptions} */
231
+ /* initOptions={initOptions} */
213
232
>
214
233
{ ({ loading , error }) => {
215
234
if (loading ) return <p >Loading</p >;
216
235
if (error ) return <p >{ error .message } </p >;
217
236
return <App />;
218
237
}}
219
238
</DittoProvider >
220
- )
221
- }
222
-
239
+ );
240
+ };
223
241
224
-
225
- ReactDOM .render (
242
+ const root = ReactDOM . createRoot ( document . getElementById ( " root " ));
243
+ root .render (
226
244
<React.StrictMode >
227
245
<RootComponent />
228
- </React.StrictMode >,
229
- document .getElementById (' root' )
246
+ </React.StrictMode >
230
247
);
231
248
```
232
249
@@ -236,45 +253,53 @@ ReactDOM.render(
236
253
import { usePendingCursorOperation , useMutations } from ' @dittolive/react-ditto' ;
237
254
238
255
export default function App() {
239
- const { documents, ditto } = usePendingCursorOperation ({
256
+ const { documents } = usePendingCursorOperation ({
240
257
collection: ' tasks' ,
241
258
});
242
259
243
- const { updateByID , upsert } = useMutations ({ collection: ' tasks' })
260
+ const { removeByID , upsert } = useMutations ({ collection: ' tasks' })
244
261
245
262
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
+ </>
253
276
)
254
277
}
255
278
```
256
279
257
280
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:
258
281
259
282
``` tsx
260
- import { usePendingCursorOperation , useMutations } from ' @dittolive/react-ditto' ;
283
+ import { useLazyPendingCursorOperation } from ' @dittolive/react-ditto' ;
261
284
262
285
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
+ );
267
294
}
268
295
269
296
return (
270
297
<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 >
275
300
))}
276
301
</ul >
277
- )
302
+ );
278
303
}
279
304
```
280
305
0 commit comments