Back to Next Js

Hello World example

examples/with-context-api/README.md

16.2.51.6 KB
Original Source

Hello World example

This example shows how to use react context api in our app.

It provides an example of using app/layout.tsx to include the context api provider and then shows how both the app/page.tsx and app/about/page.tsx can both share the same data using the context api consumer.

We start off by creating two contexts. One that actually never changes (CounterDispatchContext) and one that changes more often (CounterStateContext).

The app/page.tsx shows how to, from the home page, increment and decrement the context data by 1 (a hard code value in the context provider itself).

The app/about/page.tsx shows how to pass an increment value from the about page into the context provider itself.

Deploy your own

How to use

Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:

bash
npx create-next-app --example with-context-api with-context-api-app
bash
yarn create next-app --example with-context-api with-context-api-app
bash
pnpm create next-app --example with-context-api with-context-api-app

Deploy it to the cloud with Vercel (Documentation).