Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 842

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T02:37:08+00:00 2024-11-25T02:37:08+00:00

Next.js, integrate React Context only in certain pages

  • 62k

Next.js is one of the most popular and widely used React frameworks.

Using the Context API in Next.js is very simple but there are specific cases where you may need to wrap only certain pages by the provider.

To do this, create a new next project

npx create-next-app my-app 
Enter fullscreen mode Exit fullscreen mode

We are going to create a context that will increment and decrement a number.

Create a new folder components and inside create a new file named context.js then paste this code

import { createContext, useContext, useState, useCallback } from "react";  const Context = createContext({});  export const CountProvider = ({ children }) => {   const [count, setCount] = useState(0);    const increment = useCallback(() => {     setCount((prevState) => prevState + 1);   }, [count]);    const decrement = useCallback(() => {     setCount((prevState) => prevState - 1);   }, [count]);    return (     <Context.Provider value={{ count, increment, decrement }}>       {children}     </Context.Provider>   ); };  export function useCount() {   return useContext(Context); } 
Enter fullscreen mode Exit fullscreen mode

the custom hook useCount() will allow us to use the values passed to the context provider.

In the _app.js file, add this new component

const Noop = ({ children }) => <>{children}</>; 
Enter fullscreen mode Exit fullscreen mode

Then in MyApp component which is exported by default, we will add a prop named provider which will be accessible in all pages and its value will be <Noop/>if no context provider is passed as a value.

Now the _app.js file will be like this

import "../styles/globals.css";  const Noop = ({ children }) => <>{children}</>;  function MyApp({ Component, pageProps }) {   const ContextProvider = Component.provider || Noop;   return (     <ContextProvider>       <Component {...pageProps} />     </ContextProvider>   ); }  export default MyApp; 
Enter fullscreen mode Exit fullscreen mode

We will consume the provider in the home page like this

import styles from "../styles/Home.module.css"; import { CountProvider, useCount } from "../components/context";  export default function Home() {   const { increment, count, decrement } = useCount();   return (     <div className={styles.container}>       <button onClick={increment}>Increment</button>       <h1>{count}</h1>       <button onClick={decrement}>Decrement</button>     </div>   ); } 
Enter fullscreen mode Exit fullscreen mode

If you try it in the browser, you won't be able to increment or decrement a number because we don't have the provider as a prop in the page.

To make it work, add

Home.provider = CountProvider; 
Enter fullscreen mode Exit fullscreen mode

After reloading the page, you can increment and decrement the number.

Source code

javascriptnextjsreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.