Okay, let's get the Sonner toast component from shadcn/ui working in your JavaScript-based Next.js React project.
While shadcn/ui examples often use TypeScript, the components themselves can be added and used in JavaScript projects. The shadcn-ui CLI handles generating the component code in JavaScript if you configure it that way.
Here’s how you can add and use the Sonner toast component:
1. Initialize shadcn/ui (if you haven't already)
If this is your first time using shadcn/ui in this project, you need to initialize it. Make sure to specify that you are not using TypeScript.
npx shadcn-ui@latest init
During the initialization prompts:
- When asked
Would you like to use TypeScript (recommended)?, answer No.
- Configure the rest of the options (style, base color,
globals.css location, Tailwind config, components alias, utils alias, etc.) according to your project structure. This command will set up your components.json file and potentially create necessary utility files and CSS variables.
2. Add the sonner component
Use the shadcn-ui CLI to add the sonner component. It will read your components.json (which knows you're using JavaScript) and install the component accordingly.
npx shadcn-ui@latest add sonner
This command will typically:
- Install the underlying
sonner library (npm install sonner).
- Copy the JavaScript version of the
Sonner component code into your project, likely at components/ui/sonner.jsx (or wherever your component alias points).
3. Integrate the Toaster component into your layout
Just like the TypeScript example, you need to place the <toaster></toaster> component in your root layout file so it's available on all pages.
-
If using the App Router (app directory): Modify your app/layout.js (or layout.jsx) file.
// app/layout.js or app/layout.jsx
import { Toaster } from "@/components/ui/sonner"; // Adjust the import path based on your alias/structure
import './globals.css'; // Make sure your global styles are imported
export const metadata = {
title: 'My Next App',
description: 'Generated by create next app',
};
export default function RootLayout({ children }) {
return (
<html lang="en">
-
If using the Pages Router (pages directory): Modify your pages/_app.js (or _app.jsx) file.
// pages/_app.js or pages/_app.jsx
import { Toaster } from '@/components/ui/sonner'; // Adjust the import path
import '@/styles/globals.css'; // Adjust path to your global styles
function MyApp({ Component, pageProps }) {
return (
<>
<component {...pageprops}=""></component>
{/* Place the Toaster here */}
);
}
export default MyApp;
```
4. Trigger toasts from your components
Now you can import the toast function directly from the sonner library in any client component where you want to trigger a notification.
// components/MyExampleButton.jsx
"use client"; // Required for onClick event handlers and calling toast
import React from 'react';
import { toast } from "sonner"; // Import toast from the library itself
import { Button } from "@/components/ui/button"; // Example using shadcn button
export function MyExampleButton() {
const showToast = () => {
toast("Event has been created.");
};
const showErrorToast = () => {
toast.error("Something went wrong!");
}
return (
<div classname="space-x-2">
<button onclick="{showToast}" variant="outline">
Show Toast
</button>
<button onclick="{showErrorToast}" variant="destructive">
Show Error Toast
</button>
</div>
);
}
// Then use <myexamplebutton></myexamplebutton> somewhere in your page/component tree.
Key Points for JavaScript Usage:
"use client" Directive: Components that use hooks like useState, useEffect, or handle user interactions like onClick must have the "use client"; directive at the top of the file when using the Next.js App Router. This includes components where you call the toast() function. The <toaster></toaster> component itself also needs to run on the client, and the file generated by shadcn-ui likely includes this directive already.
- File Extensions: Use
.js or .jsx for your files.
- Import Paths: Double-check the import path for
<toaster></toaster> (@/components/ui/sonner or similar) matches where the CLI placed the component file based on your components.json configuration. The toast function itself is imported directly from the sonner package.
- No Types: You won't have TypeScript's type checking or autocompletion for props, but the components will function the same way. Refer to the Sonner and
shadcn/ui documentation for available props and options.
By following these steps, you should be able to successfully integrate and use the Sonner toast component in your JavaScript Next.js project.