Skip to content

Commit 2ef57e4

Browse files
committed
React Router v7 support
1 parent c7553e5 commit 2ef57e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+6788
-21483
lines changed

README.md

+45-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
![npm](https://img.shields.io/npm/dw/remix-toast?style=plastic)
77
![GitHub top language](https://img.shields.io/github/languages/top/Code-Forge-Net/remix-toast?style=plastic)
88

9-
Simple server-side toast management library for Remix.run!
9+
Simple server-side toast management library for React router v7 / Remix.run!
1010

1111
This library provides you with all the essential utilities that you might need to
1212
show toast notifications to your users. The client side implementation is completely
@@ -23,6 +23,24 @@ https://alemtuzlak.hashnode.dev/handling-toasts-in-remix
2323
npm install remix-toast
2424
```
2525

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+
2644
## Setup
2745

2846
### Server-side
@@ -36,7 +54,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
3654
// Extracts the toast from the request
3755
const { toast, headers } = await getToast(request);
3856
// Important to pass in the headers so the toast is cleared properly
39-
return json({ toast }, { headers });
57+
return data({ toast }, { headers });
4058
}
4159

4260
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
6179
#### react-toastify
6280

6381
```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";
6684
import { useEffect } from "react";
6785
import { getToast } from "remix-toast";
6886
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";
7088

7189
// Add the toast stylesheet
7290
export const links: LinksFunction = () => [{ rel: "stylesheet", href: toastStyles }];
7391
// Implemented from above
7492
export const loader = async ({ request }: LoaderFunctionArgs) => {
7593
const { toast, headers } = await getToast(request);
76-
return json({ toast }, { headers });
94+
return data({ toast }, { headers });
7795
};
7896

7997
export default function App() {
@@ -106,16 +124,16 @@ export default function App() {
106124
#### Sonner
107125

108126
```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";
111129
import { useEffect } from "react";
112130
import { getToast } from "remix-toast";
113131
import { Toaster, toast as notify } from "sonner";
114132

115133
// Implemented from above
116134
export const loader = async ({ request }: LoaderFunctionArgs) => {
117135
const { toast, headers } = await getToast(request);
118-
return json({ toast }, { headers });
136+
return data({ toast }, { headers });
119137
};
120138

121139
export default function App() {
@@ -165,7 +183,7 @@ setToastCookieOptions({
165183
`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.
166184

167185
```tsx
168-
import { createCookieSessionStorage } from "@remix-run/node";
186+
import { createCookieSessionStorage } from "react-router";
169187
import { createToastUtilsWithCustomSession } from "remix-toast";
170188

171189
const session = createCookieSessionStorage({
@@ -182,10 +200,10 @@ export const {
182200
redirectWithError,
183201
redirectWithInfo,
184202
redirectWithWarning,
185-
jsonWithSuccess,
186-
jsonWithError,
187-
jsonWithInfo,
188-
jsonWithWarning
203+
dataWithSuccess,
204+
dataWithError,
205+
dataWithInfo,
206+
dataWithWarning
189207
} = createToastUtilsWithCustomSession(session);
190208
```
191209

@@ -256,53 +274,53 @@ export const action = () => {
256274
};
257275
```
258276

259-
### jsonWithSuccess
277+
### dataWithSuccess
260278

261279
Display a success toast message without a redirection.
262280

263281
```tsx
264-
import { jsonWithSuccess } from "remix-toast";
282+
import { dataWithSuccess } from "remix-toast";
265283

266284
export const action = () => {
267-
return jsonWithSuccess({ result: "Data saved successfully" }, "Operation successful! 🎉");
285+
return dataWithSuccess({ result: "Data saved successfully" }, "Operation successful! 🎉");
268286
//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" });
270288
};
271289
```
272290

273-
### jsonWithError
291+
### dataWithError
274292

275293
Display an error toast message without a redirection.
276294

277295
```tsx
278-
import { jsonWithError } from "remix-toast";
296+
import { dataWithError } from "remix-toast";
279297

280298
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.");
282300
};
283301
```
284302

285-
### jsonWithInfo
303+
### dataWithInfo
286304

287305
Display an info toast message without a redirection.
288306

289307
```tsx
290-
import { jsonWithInfo } from "remix-toast";
308+
import { dataWithInfo } from "remix-toast";
291309

292310
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.");
294312
};
295313
```
296314

297-
### jsonWithWarning
315+
### dataWithWarning
298316

299317
Display a warning toast message without a redirection.
300318

301319
```tsx
302-
import { jsonWithWarning } from "remix-toast";
320+
import { dataWithWarning } from "remix-toast";
303321

304322
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.");
306324
};
307325
```
308326

0 commit comments

Comments
 (0)