-
Notifications
You must be signed in to change notification settings - Fork 243
/
react-comments.mdx
219 lines (176 loc) · 6.27 KB
/
react-comments.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
meta:
title: "Get started with Comments using Liveblocks and React"
parentTitle: "Get started"
description:
"Learn how to get started with Comments using Liveblocks and React"
---
Liveblocks is a realtime collaboration infrastructure for building performant
collaborative experiences. Follow the following steps to start adding a
commenting experience to your React application using the hooks from
[`@liveblocks/react`](/docs/api-reference/liveblocks-react) and the components
from [`@liveblocks/react-ui`](/docs/api-reference/liveblocks-react-ui).
## Quickstart
<Steps>
<Step>
<StepTitle>Install Liveblocks</StepTitle>
<StepContent>
Every package should use the same version.
```bash
npm install @liveblocks/client @liveblocks/react @liveblocks/react-ui
```
</StepContent>
</Step>
<Step>
<StepTitle>Initialize the `liveblocks.config.ts` file</StepTitle>
<StepContent>
We can use this file later to [define types for our application](/docs/api-reference/liveblocks-react#Typing-your-data).
```bash
npx create-liveblocks-app@latest --init --framework react
```
</StepContent>
</Step>
<Step>
<StepTitle>Set up the Liveblocks client</StepTitle>
<StepContent>
Liveblocks uses the concept of rooms, separate virtual spaces where people
collaborate, and to create a realtime experience, multiple users must
be connected to the same room. Set up a Liveblocks client with [`LiveblocksProvider`](/docs/api-reference/liveblocks-react#LiveblocksProvider), and join a room with [`RoomProvider`](/docs/api-reference/liveblocks-react#RoomProvider).
```tsx file="App.tsx" highlight="11-15"
"use client";
import {
LiveblocksProvider,
RoomProvider,
} from "@liveblocks/react/suspense";
import { Room } from "./Room";
export default function App() {
return (
<LiveblocksProvider publicApiKey={"{{PUBLIC_KEY}}"}>
<RoomProvider id="my-room" initialPresence={{}}>
{/* ... */}
</RoomProvider>
</LiveblocksProvider>
);
}
```
</StepContent>
</Step>
<Step>
<StepTitle>Join a Liveblocks room</StepTitle>
<StepContent>
After setting up the room, you can add collaborative components inside it, using
[`ClientSideSuspense`](/docs/api-reference/liveblocks-react#ClientSideSuspense) to add loading spinners to your app.
```tsx file="App.tsx" highlight="14-16"
"use client";
import {
LiveblocksProvider,
RoomProvider,
ClientSideSuspense,
} from "@liveblocks/react/suspense";
import { Room } from "./Room";
export default function App() {
return (
<LiveblocksProvider publicApiKey={"{{PUBLIC_KEY}}"}>
<RoomProvider id="my-room" initialPresence={{}}>
<ClientSideSuspense fallback={<div>Loading…</div>}>
<Room />
</ClientSideSuspense>
</RoomProvider>
</LiveblocksProvider>
);
}
```
</StepContent>
</Step>
<Step>
<StepTitle>Use the Liveblocks hooks and components</StepTitle>
<StepContent>
Now that we’re connected to a room, we can start using the Liveblocks hooks and components.
We’ll add [`useThreads`](/docs/api-reference/liveblocks-react#useThreads) to get the threads in the room, then we’ll use the [`Thread`](/docs/api-reference/liveblocks-react-ui#Thread) component
to render them. Finally, we’ll add a way to create threads by adding a [`Composer`](/docs/api-reference/liveblocks-react-ui#Composer).
```tsx file="Room.tsx" highlight="7,11-14"
"use client";
import { useThreads } from "@liveblocks/react/suspense";
import { Composer, Thread } from "@liveblocks/react-ui";
export function Room() {
const { threads } = useThreads();
return (
<div>
{threads.map((thread) => (
<Thread key={thread.id} thread={thread} />
))}
<Composer />
</div>
);
}
```
</StepContent>
</Step>
<Step>
<StepTitle>Import default styles</StepTitle>
<StepContent>
The default components come with default styles, you can import them into the
root of your app or directly into a CSS file with `@import`.
```tsx
import "@liveblocks/react-ui/styles.css";
```
</StepContent>
</Step>
<Step lastStep>
<StepTitle>Next: authenticate and add your users</StepTitle>
<StepContent>
Comments is set up and working now, but each user is anonymous—the next step is to
authenticate each user as they connect, and attach their name and avatar to their comments.
<Button asChild className="not-markdown">
<a href="/docs/guides/how-to-add-users-to-liveblocks-comments">
Add your users to Comments
</a>
</Button>
</StepContent>
</Step>
</Steps>
## What to read next
Congratulations! You’ve set up the foundation to start building a commenting
experience for your React application.
- [API Reference](/docs/api-reference/liveblocks-react-ui)
- [How to send email notifications when comments are created](/docs/guides/how-to-send-email-notifications-when-comments-are-created)
---
## Examples using React
<ListGrid columns={2}>
<ExampleCard
example={{
title: "Comments",
slug: "comments/react-ui",
image: "/images/examples/thumbnails/comments.jpg",
}}
technologies={["nextjs", "react"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Video Comments",
slug: "video-comments/nextjs-comments-video",
image: "/images/examples/thumbnails/comments-video.png",
}}
technologies={["nextjs"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Overlay Comments",
slug: "overlay-comments/nextjs-comments-overlay",
image: "/images/examples/thumbnails/comments-overlay.png",
}}
technologies={["nextjs"]}
openInNewWindow
/>
<ExampleCard
example={{
title: "Text Editor Comments",
slug: "text-editor-comments/nextjs-comments-tiptap",
image: "/images/examples/thumbnails/comments-text-editor.png",
}}
technologies={["nextjs"]}
openInNewWindow
/>
</ListGrid>