6
6
![ npm] ( https://img.shields.io/npm/dw/remix-toast?style=plastic )
7
7
![ GitHub top language] ( https://img.shields.io/github/languages/top/Code-Forge-Net/remix-toast?style=plastic )
8
8
9
- Simple server-side toast management library for Remix.run!
9
+ Simple server-side toast management library for React router v7 / Remix.run!
10
10
11
11
This library provides you with all the essential utilities that you might need to
12
12
show toast notifications to your users. The client side implementation is completely
@@ -23,6 +23,24 @@ https://alemtuzlak.hashnode.dev/handling-toasts-in-remix
23
23
npm install remix-toast
24
24
```
25
25
26
+ ## Remix.run
27
+
28
+ If you are using Remix.run you can use v1.2.2 of this library or lower. V2 onwards is only react-router v7 compatible.
29
+
30
+ ### Migration guide to react-router v7
31
+
32
+ If you are using react-router v7 you can use v2.0.0 of this library. The only thing you have to change is rename all the ` json ` methods to ` data ` methods, the redirect methods stayed the same. For example:
33
+
34
+ ``` diff
35
+ - import { jsonWithSuccess } from "remix-toast";
36
+ + import { dataWithSuccess } from "remix-toast";
37
+
38
+ export const action = () => {
39
+ - return jsonWithSuccess({ result: "Data saved successfully" }, "Operation successful! 🎉");
40
+ + return dataWithSuccess({ result: "Data saved successfully" }, "Operation successful! 🎉");
41
+ };
42
+ ```
43
+
26
44
## Setup
27
45
28
46
### Server-side
@@ -36,7 +54,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
36
54
// Extracts the toast from the request
37
55
const { toast, headers } = await getToast (request );
38
56
// Important to pass in the headers so the toast is cleared properly
39
- return json ({ toast }, { headers });
57
+ return data ({ toast }, { headers });
40
58
}
41
59
42
60
export default function App({ children }: { children: ReactNode }) {
@@ -61,19 +79,19 @@ After this you can then use any toast notification library you prefer, but here
61
79
#### react-toastify
62
80
63
81
``` tsx
64
- import { json , type LinksFunction , type LoaderFunctionArgs } from " @remix-run/node " ;
65
- import { Links , LiveReload , Meta , Outlet , Scripts , ScrollRestoration , useLoaderData } from " @remix-run/ react" ;
82
+ import { data , type LinksFunction , type LoaderFunctionArgs } from " react-router " ;
83
+ import { Links , LiveReload , Meta , Outlet , Scripts , ScrollRestoration , useLoaderData } from " react-router " ;
66
84
import { useEffect } from " react" ;
67
85
import { getToast } from " remix-toast" ;
68
86
import { ToastContainer , toast as notify } from " react-toastify" ;
69
- import toastStyles from " react-toastify/dist/ ReactToastify.css" ;
87
+ import toastStyles from " react-toastify/ReactToastify.css?url " ;
70
88
71
89
// Add the toast stylesheet
72
90
export const links: LinksFunction = () => [{ rel: " stylesheet" , href: toastStyles }];
73
91
// Implemented from above
74
92
export const loader = async ({ request }: LoaderFunctionArgs ) => {
75
93
const { toast, headers } = await getToast (request );
76
- return json ({ toast }, { headers });
94
+ return data ({ toast }, { headers });
77
95
};
78
96
79
97
export default function App() {
@@ -106,16 +124,16 @@ export default function App() {
106
124
#### Sonner
107
125
108
126
``` tsx
109
- import { json , type LinksFunction , type LoaderFunctionArgs } from " @remix-run/node " ;
110
- import { Links , LiveReload , Meta , Outlet , Scripts , ScrollRestoration , useLoaderData } from " @remix-run/ react" ;
127
+ import { data , type LinksFunction , type LoaderFunctionArgs } from " react-router " ;
128
+ import { Links , LiveReload , Meta , Outlet , Scripts , ScrollRestoration , useLoaderData } from " react-router " ;
111
129
import { useEffect } from " react" ;
112
130
import { getToast } from " remix-toast" ;
113
131
import { Toaster , toast as notify } from " sonner" ;
114
132
115
133
// Implemented from above
116
134
export const loader = async ({ request }: LoaderFunctionArgs ) => {
117
135
const { toast, headers } = await getToast (request );
118
- return json ({ toast }, { headers });
136
+ return data ({ toast }, { headers });
119
137
};
120
138
121
139
export default function App() {
@@ -165,7 +183,7 @@ setToastCookieOptions({
165
183
` createToastUtilsWithCustomSession ` is a function that allows you to create a custom session for your toasts. This is useful if you want to have different types of toasts for different parts of your app.
166
184
167
185
``` tsx
168
- import { createCookieSessionStorage } from " @remix-run/node " ;
186
+ import { createCookieSessionStorage } from " react-router " ;
169
187
import { createToastUtilsWithCustomSession } from " remix-toast" ;
170
188
171
189
const session = createCookieSessionStorage ({
@@ -182,10 +200,10 @@ export const {
182
200
redirectWithError,
183
201
redirectWithInfo,
184
202
redirectWithWarning,
185
- jsonWithSuccess ,
186
- jsonWithError ,
187
- jsonWithInfo ,
188
- jsonWithWarning
203
+ dataWithSuccess ,
204
+ dataWithError ,
205
+ dataWithInfo ,
206
+ dataWithWarning
189
207
} = createToastUtilsWithCustomSession (session );
190
208
```
191
209
@@ -256,53 +274,53 @@ export const action = () => {
256
274
};
257
275
```
258
276
259
- ### jsonWithSuccess
277
+ ### dataWithSuccess
260
278
261
279
Display a success toast message without a redirection.
262
280
263
281
``` tsx
264
- import { jsonWithSuccess } from " remix-toast" ;
282
+ import { dataWithSuccess } from " remix-toast" ;
265
283
266
284
export const action = () => {
267
- return jsonWithSuccess ({ result: " Data saved successfully" }, " Operation successful! 🎉" );
285
+ return dataWithSuccess ({ result: " Data saved successfully" }, " Operation successful! 🎉" );
268
286
// or with description and message (works for all the other utilities as well)
269
- return jsonWithSuccess ({ result: " Data saved successfully" }, { message: " Operation successful! 🎉" , description: " description of toast" });
287
+ return dataWithSuccess ({ result: " Data saved successfully" }, { message: " Operation successful! 🎉" , description: " description of toast" });
270
288
};
271
289
```
272
290
273
- ### jsonWithError
291
+ ### dataWithError
274
292
275
293
Display an error toast message without a redirection.
276
294
277
295
``` tsx
278
- import { jsonWithError } from " remix-toast" ;
296
+ import { dataWithError } from " remix-toast" ;
279
297
280
298
export const action = () => {
281
- return jsonWithError (null , " Oops! Something went wrong. Please try again later." );
299
+ return dataWithError (null , " Oops! Something went wrong. Please try again later." );
282
300
};
283
301
```
284
302
285
- ### jsonWithInfo
303
+ ### dataWithInfo
286
304
287
305
Display an info toast message without a redirection.
288
306
289
307
``` tsx
290
- import { jsonWithInfo } from " remix-toast" ;
308
+ import { dataWithInfo } from " remix-toast" ;
291
309
292
310
export const action = () => {
293
- return jsonWithInfo ({ info: " Additional information" }, " Your profile has been successfully updated." );
311
+ return dataWithInfo ({ info: " Additional information" }, " Your profile has been successfully updated." );
294
312
};
295
313
```
296
314
297
- ### jsonWithWarning
315
+ ### dataWithWarning
298
316
299
317
Display a warning toast message without a redirection.
300
318
301
319
``` tsx
302
- import { jsonWithWarning } from " remix-toast" ;
320
+ import { dataWithWarning } from " remix-toast" ;
303
321
304
322
export const action = () => {
305
- return jsonWithWarning ({ warning: " Potential issues" }, " Your session is about to expire." );
323
+ return dataWithWarning ({ warning: " Potential issues" }, " Your session is about to expire." );
306
324
};
307
325
```
308
326
0 commit comments