ReactJS Program to Language Switcher With Context with Explanation
ReactJS
Hard
State Management
25 views
1 min read
88 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around language, context, switcher. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create a language switcher using context and show translated text.
Input Format
No input.
Output Format
Render a React component.
Constraints
Provide language in context and read it in children.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { createContext, useContext, useMemo, useState } from 'react';
const LangContext = createContext(null);
const strings = {
en: { title: 'Practice today on meetcode', button: 'Switch to Hindi' },
hi: { title: 'Aaj meetcode par practice karo', button: 'Switch to English' }
};
function LangProvider({ children }) {
const [lang, setLang] = useState('en');
const value = useMemo(() => ({ lang, toggle: () => setLang((l) => (l === 'en' ? 'hi' : 'en')) }), [lang]);
return <LangContext.Provider value={value}>{children}</LangContext.Provider>;
}
function useLang() {
const ctx = useContext(LangContext);
if (!ctx) throw new Error('LangProvider missing');
return ctx;
}
function Card() {
const { lang, toggle } = useLang();
return (
<div style={{ border: '1px solid #eee', borderRadius: 16, padding: 16, width: 480 }}>
<h2 style={{ marginTop: 0 }}>{strings[lang].title}</h2>
<button type='button' onClick={toggle} style={{ padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>{strings[lang].button}</button>
</div>
);
}
export default function App() {
return (
<div style={{ padding: 16 }}>
<LangProvider>
<Card />
</LangProvider>
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Create a language switcher using context and show translated text.
Input / Output
Output
Render a React component.
Constraints
Provide language in context and read it in children.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
import React, { createContext, useContext, useMemo, useState } from 'react';
const LangContext = createContext(null);
const strings = {
en: { title: 'Practice today on meetcode', button: 'Switch to Hindi' },
hi: { title: 'Aaj meetcode par practice karo', button: 'Switch to English' }
};
function LangProvider({ children }) {
const [lang, setLang] = useState('en');
const value = useMemo(() => ({ lang, toggle: () => setLang((l) => (l === 'en' ? 'hi' : 'en')) }), [lang]);
return <LangContext.Provider value={value}>{children}</LangContext.Provider>;
}
function useLang() {
const ctx = useContext(LangContext);
if (!ctx) throw new Error('LangProvider missing');
return ctx;
}
function Card() {
const { lang, toggle } = useLang();
return (
<div style={{ border: '1px solid #eee', borderRadius: 16, padding: 16, width: 480 }}>
<h2 style={{ marginTop: 0 }}>{strings[lang].title}</h2>
<button type='button' onClick={toggle} style={{ padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>{strings[lang].button}</button>
</div>
);
}
export default function App() {
return (
<div style={{ padding: 16 }}>
<LangProvider>
<Card />
</LangProvider>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!