📖 Scenario: You are building a simple Next.js app where a user can toggle a theme between light and dark modes. The theme state should stay synchronized across components and persist on the client side.
🎯 Goal: Create a Next.js app that uses state synchronization patterns to share and update a theme state between components using React context and hooks.
📋 What You'll Learn
Create a React context to hold the theme state
Add a provider component to wrap the app and hold the theme state
Use a toggle function to switch between 'light' and 'dark' themes
Consume the theme state and toggle function in a child component
💡 Why This Matters
🌍 Real World
Many apps need to share state like themes, user info, or settings across multiple components without passing props manually. This pattern helps keep UI consistent and synchronized.
💼 Career
Understanding state synchronization with React context and hooks is essential for building scalable React and Next.js applications used in professional web development.
Progress0 / 4 steps
1
Create the Theme Context
Create a file called ThemeContext.js and inside it, create a React context called ThemeContext using createContext from react. Initialize it with null.
NextJS
Hint
Use import { createContext } from 'react' and then const ThemeContext = createContext(null).
2
Add ThemeProvider Component
In ThemeContext.js, create a functional component called ThemeProvider that uses useState to hold a theme state initialized to 'light'. Provide theme and a toggleTheme function that switches between 'light' and 'dark' via ThemeContext.Provider. Export ThemeProvider.
NextJS
Hint
Use useState('light') and create a toggleTheme function that switches the theme. Wrap {children} with ThemeContext.Provider.
3
Consume Theme Context in a Component
Create a new component called ThemeSwitcher.js. Inside it, use useContext to get theme and toggleTheme from ThemeContext. Render a button that shows the current theme and calls toggleTheme on click.
NextJS
Hint
Use const { theme, toggleTheme } = useContext(ThemeContext) and render a button that calls toggleTheme on click.
4
Wrap App with ThemeProvider
In app/layout.js (or your root layout file), import ThemeProvider from ThemeContext.js and wrap the {children} with <ThemeProvider> so the theme state is available throughout the app.
NextJS
Hint
Wrap {children} inside <ThemeProvider> tags in the root layout.
Practice
(1/5)
1. What is the main purpose of state synchronization in Next.js applications?
easy
A. To keep data consistent between server and client components
B. To improve CSS styling performance
C. To handle routing between pages
D. To optimize image loading speed
Solution
Step 1: Understand state synchronization concept
State synchronization means keeping data the same across different parts of an app, especially between server and client.
Step 2: Identify Next.js usage
Next.js apps often fetch data on the server and need to keep that data consistent on the client side.
Final Answer:
To keep data consistent between server and client components -> Option A
Quick Check:
State sync = consistent data [OK]
Hint: Think about data staying the same everywhere [OK]
Common Mistakes:
Confusing state sync with styling or routing
Thinking it only applies to client-side
Assuming it improves image loading
2. Which of the following is the correct way to update a React state variable count using the useState hook in Next.js?
easy
A. updateCount(count + 1);
B. count = count + 1;
C. useState(count + 1);
D. setCount(count + 1);
Solution
Step 1: Recall useState update syntax
React's useState returns a setter function to update state, e.g., setCount(newValue).
Step 2: Identify correct usage
Direct assignment like count = count + 1 is invalid; useState(count + 1) is a hook call, not update; updateCount is undefined.
Final Answer:
setCount(count + 1); -> Option D
Quick Check:
useState setter = setCount() [OK]
Hint: Use the setter function from useState to update state [OK]