0
0
NextJSframework~10 mins

Language switching UI in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Language switching UI
User sees UI with current language
User clicks language button
Update language state
Re-render UI with new language
Display content in selected language
The UI shows the current language. When the user clicks a language button, the app updates the language state and re-renders the content in the chosen language.
Execution Sample
NextJS
"use client";
import { useState } from 'react';

export default function LangSwitcher() {
  const [lang, setLang] = useState('en');
  const content = lang === 'en' ? 'Content in English' : 'Contenido en Español';
  return (
    <div>
      <p>{content}</p>
      <button onClick={() => setLang(lang === 'en' ? 'es' : 'en')}>Switch</button>
    </div>
  );
}
A simple button toggles language state between English and Spanish on click.
Execution Table
StepUI Language StateUser ActionState ChangeUI Output
1enPage loadsNo changeButton shows 'Switch' (content in English)
2enUser clicks buttonlang changes to 'es'UI re-renders, content in Spanish
3esUser clicks buttonlang changes to 'en'UI re-renders, content in English
4enNo further clicksNo changeUI remains in English
💡 User stops clicking; language state remains stable.
Variable Tracker
VariableStartAfter 1After 2After 3Final
lang'en''en''es''en''en'
Key Moments - 2 Insights
Why does the UI update immediately after clicking the button?
Because clicking triggers setLang which updates the state, causing React to re-render the component with the new language (see execution_table step 2).
What happens if we don't use state for language?
Without state, React won't know to re-render the UI when language changes, so the displayed language won't update (state change is key as shown in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'lang' after the second click?
A'en'
B'es'
Cundefined
D'fr'
💡 Hint
Check the 'After 3' column in the variable_tracker.
At which step does the UI first show content in Spanish?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'UI Output' column in the execution_table for when Spanish content appears.
If the initial state was 'es' instead of 'en', what would the UI show on page load?
AEnglish content
BNo content
CSpanish content
DError message
💡 Hint
Refer to the 'Start' value of 'lang' in variable_tracker and how UI output depends on it.
Concept Snapshot
Language switching UI in Next.js:
- Use useState to hold current language
- Render UI based on language state
- On button click, update language state
- React re-renders UI with new language
- State change triggers visible language update
Full Transcript
This example shows a simple language switching UI in Next.js using React's useState hook. Initially, the language state is set to English ('en'). The UI displays a button labeled 'Switch'. When the user clicks this button, the language state toggles between 'en' and 'es' (Spanish). Each click updates the state, causing React to re-render the component and display content in the selected language. The execution table traces each step: starting with English, switching to Spanish on the first click, back to English on the second click, and so on. The variable tracker shows how the 'lang' variable changes over time. Key moments highlight why state is necessary for UI updates and how React re-renders on state change. The quiz questions test understanding of state changes and UI output at each step. This pattern is common for building accessible, responsive language switchers in modern web apps.