With Next.js

Setting up PrimeReact in a Next.js project.

Working Example#

Quickstart your project with our PrimeReact + Next.js template
Use this Next.js template pre-configured with PrimeReact to quickly start building your app with ready-to-use UI components and styles.
github.com/primefaces/primereact-examples

Installation#

Install Packages#

Install PrimeReact and a theme package using your favorite package manager:

npm install [email protected] @primeuix/themes

PrimeReactProvider#

Create a prime-ssr-provider.tsx file in the root of your project and add the following code:

'use client';
import { PrimeReactProvider, PrimeReactStyleSheet } from '@primereact/core';
import { useServerInsertedHTML } from 'next/navigation';
import * as React from 'react';
import Aura from '@primeuix/themes/aura';
 
const styledStyleSheet = new PrimeReactStyleSheet();
 
export default function PrimeSSRProvider({
    children
}: Readonly<{
    children?: React.ReactNode;
}>) {
    useServerInsertedHTML(() => {
        const styleElements = styledStyleSheet.getAllElements();
 
        styledStyleSheet.clear();
 
        return <>{styleElements}</>;
    });
 
    const primereact = {
        theme: {
            preset: Aura
        }
    };
 
    return (
        <PrimeReactProvider {...primereact} stylesheet={styledStyleSheet}>
            {children}
        </PrimeReactProvider>
    );
}

Add SSRProvider#

Import the PrimeSSRProvider in your root layout.tsx file and wrap your app with it.

...
import PrimeSSRProvider from './prime-ssr-provider';
 
export default function RootLayout({
    children
}: Readonly<{
    children: React.ReactNode;
}>) {
    return (
        <html lang="en">
            <body>
                <PrimeSSRProvider>{children}</PrimeSSRProvider>
            </body>
        </html>
    );
}

Verify#

To verify that PrimeReact is installed correctly, you can create a simple component such as Button and render it in your application. Each component can be imported and registered individually so that you only include what you use for bundle optimization. Import path is available in the documentation of the corresponding component.

import { Button } from 'primereact/button';
 
export default function VerifyInstallation() {
    return (
        <div className="card flex justify-center">
            <Button>Verify</Button>
        </div>
    );
}

More Tips#

  • You can import and use only the components you need for a smaller bundle size.
  • For icons, custom themes, and advanced setup, see the PrimeReact documentation.

Troubleshooting#

If you encounter issues during installation or setup, check the following:

  • Ensure that you have the latest version of Vite and Node.js installed.
  • Verify that your project structure is correct and that the PrimeReactProvider is properly wrapped around your application.
  • Check the browser console for any errors related to PrimeReact components or themes.
  • If you are using TypeScript, ensure that you have the necessary type definitions installed.
  • Refer to the PrimeReact GitHub repository for more information and support.