-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemix Basics.txt
More file actions
210 lines (156 loc) Β· 5.39 KB
/
Remix Basics.txt
File metadata and controls
210 lines (156 loc) Β· 5.39 KB
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
Architecuture
-----------------
- Remix has (3) builds associated with it
1. /server-build - built by converting /server/index.ts into a javacript bundle
2. /build - this is a bundle that contains all of the server-side code/routes
3. /public - this a bundle that contains all client-side code
[ Entry Points ]
Server -> /app/entry.server.ts
Client -> /app/entry.client.ts
Styling
---------
Routing
----------
+ = there is an expected url segment that follows after
$ = dynamic URL parameter
. =
- npx remix routes // shows Remix's understanding of the current route heiarchy within the app based on your folder structure
app
routes
users+
$username_+
notes.$noteId.tsx
notes.tsx
$username.tsx
users+ -> $username.tsx = /users/<USERNAME>
users+ -> $username_+ -> notes.$noteId.tsx = /users/<USERNAME>/notes/<NOTEID>
Rendering Child Components
----------------------------------------
Fetching Data For a Component
------------------------------------------
- loader() handles all GET requests
- in other words, it is responsible for getting data from third party APIs or internal endpoints and passing that to your components to use
export async function loader({ params }: DataFunctionArgs) {
const note = db.note.findFirst({
where: {
id: {
equals: params.noteId,
},
},
})
invariantResponse(note, 'Note not found', { status: 404 })
return json({
note: { title: note.title, content: note.content },
})
}
Handling POST/PATCH Operations
----------------------------------------------
- actions() handle all other operations (POST, PATCH, DELETE, etc)
- remix convention is that all mutation operations should be wrapped in a FORM component
- FORM components can be configured to execute a POST operation which will be handled by a support action function
export async function action({ params }: DataFunctionArgs) {
console.log(`Deleting note ${params.noteId}`)
db.note.delete({ where: { id: { equals: params.noteId } } })
console.log('Note deleted')
return redirect(`/users/${params.username}/notes`)
}
<Form method="POST">
<Button
// π¨ add a type="submit" prop to this Button
variant="destructive"
>
Delete
</Button>
</Form>
- we can only export ONE action function
- we can handle multiple actions within the single action function we can define
export async function action({ request, params }: DataFunctionArgs) {
// π¨ get the formData from the request
// π¨ get the intent from the formData
const formData = await request.formData()
const intent = formData.get('intent')
// π¨ if the intent is "delete" then proceed
if (intent === 'delete') {
db.note.delete({ where: { id: { equals: params.noteId } } })
return redirect(`/users/${params.username}/notes`)
}
// π¨ if the intent is not, then throw a 400 Response
invariantResponse(intent === 'delete', 'Something went wrong!', {
status: 400,
})
}
<Form method="POST">
<Button
type="submit"
variant="destructive"
// this metadata clearly defiines that function with the action handler that I want to invoke
// π¨ add a name="intent" and value="delete" to this button
name="intent"
value="delete "
>
Delete
</Button>
<Button
type="submit"
variant="awesome"
// this metadata clearly defiines that function with the action handler that I want to invoke
name="intent"
value="favorite "
>
Favorite
</Button>
</Form>
Pre-Fetching
----------------
- remix has a capability where if we specifiy a prefetch property on a "link" element, it will invoke all of the javascript that is associated with that link basd on it's route
- this makes the browsing experiecne feel super fast
<Link prefetch="intent" to="notes" className="underline">
*intent functions such that if the user "hoovers" over the link, Remix will go and fetch that data for that route
Error Handling
--------------------
- the ErrorBoundary component can be exported per page/route to define error handling mechanics for that particular component
-
// username.tsx
export async function loader({ params }: DataFunctionArgs) {
// throw new Error('π¨ Component error')
const user = db.user.findFirst({
where: {
username: {
equals: params.username,
},
},
})
invariantResponse(user, 'User not found', { status: 404 })
return json({
user: { name: user.name, username: user.username },
})
}
export default function Home() {
const data = useLoaderData<typeof loader>()
return (
<div className="container mb-48 mt-36">
<h1 className="text-h1">{data.user.name ?? data.user.username}</h1>
<Link to="notes" className="underline" prefetch="intent">
Notes
</Link>
</div>
)
}
export function ErrorBoundary() {
const error = useRouteError()
const params = useParams()
console.error(error)
if (isRouteErrorResponse(error) && error.status === 404) {
return (
<div className="container mx-auto flex h-full w-full items-center justify-center bg-destructive p-20 text-h2 text-destructive-foreground">
<p>User {params.username} does not exist!</p>
</div>
)
}
return (
<div className="container mx-auto flex h-full w-full items-center justify-center bg-destructive p-20 text-h2 text-destructive-foreground">
{/* π¨ display the error message here */}
<p>Oh no, something went wrong. Sorry about that.</p>
</div>
)
}