|
| 1 | +# Usage with NextJS |
| 2 | + |
| 3 | +This will guide you through a way to setup `react-scroll-parallax` using the [Next 13](https://nextjs.org/blog/next-13) App router. |
| 4 | + |
| 5 | +## Setting Up the ParallaxProvider |
| 6 | + |
| 7 | +[Next 13](https://nextjs.org/blog/next-13) requires client components to be marked with `use-client`, and the `ParallaxProvider` is a client component. In order to use the providers in your app, first create a `providers.tsx` file in the root of the app directory: |
| 8 | + |
| 9 | +``` |
| 10 | +next-app |
| 11 | +└── app/ |
| 12 | + └── providers.tsx |
| 13 | +``` |
| 14 | + |
| 15 | +In this file wrap the children in a `<ParallaxProvider>` and mark it as a client component. |
| 16 | + |
| 17 | +:::info |
| 18 | +You may add any additional providers you need for your app here in the future. |
| 19 | +::: |
| 20 | + |
| 21 | +```tsx |
| 22 | +'use client'; |
| 23 | + |
| 24 | +import { ParallaxProvider } from 'react-scroll-parallax'; |
| 25 | + |
| 26 | +export function Providers({ children }: { children: React.ReactNode }) { |
| 27 | + return <ParallaxProvider>{children}</ParallaxProvider>; |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Add the Providers to the Root Layout |
| 32 | + |
| 33 | +In the `layout.tsx` file at the root of the app directory, import the `Providers` component you just created and wrap it around the children. |
| 34 | + |
| 35 | +``` |
| 36 | +next-app |
| 37 | +└── app/ |
| 38 | + ├── providers.tsx |
| 39 | + └── layout.tsx |
| 40 | +``` |
| 41 | + |
| 42 | +Your layout may differ from the one below — it's is simplified for this example. |
| 43 | + |
| 44 | +```tsx |
| 45 | +import { Providers } from './Providers'; |
| 46 | + |
| 47 | +export default function RootLayout({ |
| 48 | + children, |
| 49 | +}: { |
| 50 | + children: React.ReactNode; |
| 51 | +}) { |
| 52 | + return ( |
| 53 | + <html lang="en"> |
| 54 | + <body> |
| 55 | + <Providers>{children}</Providers> |
| 56 | + </body> |
| 57 | + </html> |
| 58 | + ); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Create a Parallax Effect |
| 63 | + |
| 64 | +Next we can create a simple parallax effect in another client component. Add a `test.tsx` file and place a parallax effect within it. We must also make this as a client component with `use client`. |
| 65 | + |
| 66 | +``` |
| 67 | +next-app |
| 68 | +└── app/ |
| 69 | + ├── providers.tsx |
| 70 | + ├── layout.tsx |
| 71 | + ├── test.tsx |
| 72 | + └── page.tsx |
| 73 | +``` |
| 74 | + |
| 75 | +```tsx |
| 76 | +'use client'; |
| 77 | + |
| 78 | +import { Parallax } from 'react-scroll-parallax'; |
| 79 | + |
| 80 | +export default function Test() { |
| 81 | + return ( |
| 82 | + <Parallax scale={[1, 0]}> |
| 83 | + <div className="w-48 h-48 bg-red-500" /> |
| 84 | + </Parallax> |
| 85 | + ); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Use the Parallax within a Page |
| 90 | + |
| 91 | +To add parallax effects to a page, create a `page.tsx` at the route you need. In this example we'll use the homepage so the page exists at the root of the app directory. |
| 92 | + |
| 93 | +``` |
| 94 | +next-app |
| 95 | +└── app/ |
| 96 | + ├── providers.tsx |
| 97 | + ├── layout.tsx |
| 98 | + └── page.tsx |
| 99 | +``` |
| 100 | + |
| 101 | +Add the `<Test>` component and style the page so it scrolls. |
| 102 | + |
| 103 | +```tsx |
| 104 | +import Test from './Test'; |
| 105 | + |
| 106 | +export default function Page() { |
| 107 | + return ( |
| 108 | + <main className="flex min-h-[300vh] flex-col items-center justify-between p-24"> |
| 109 | + <Test /> |
| 110 | + </main> |
| 111 | + ); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +That's it. Now that `react-scroll-parallax` is working in your NextJS application you can add your own effects. See more on [how it works](/docs/examples/how-it-works). |
0 commit comments