ReactJS Program to Persist Toggle Setting with Explanation
ReactJS
Hard
State Management
30 views
1 min read
87 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around setting, localstorage, react. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Save a boolean setting in localStorage and restore it on load.
Input Format
No input.
Output Format
Render a React component.
Constraints
Read localStorage in initializer and write on changes.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useEffect, useState } from 'react';
export default function App() {
const [compact, setCompact] = useState(() => {
try {
return localStorage.getItem('meetcode_compact') === '1';
} catch {
return false;
}
});
useEffect(() => {
try {
localStorage.setItem('meetcode_compact', compact ? '1' : '0');
} catch {}
}, [compact]);
return (
<div style={{ padding: 16 }}>
<h2 style={{ marginTop: 0 }}>meetcode settings</h2>
<label style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
<input type='checkbox' checked={compact} onChange={(e) => setCompact(e.target.checked)} />
Compact mode
</label>
<div style={{ marginTop: 12, border: '1px solid #eee', borderRadius: 16, padding: compact ? 10 : 18 }}>
<strong>Preview card</strong>
<div style={{ color: '#555', marginTop: 8 }}>Padding changes based on setting.</div>
</div>
</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
Save a boolean setting in localStorage and restore it on load.
Input / Output
Output
Render a React component.
Constraints
Read localStorage in initializer and write on changes.
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, { useEffect, useState } from 'react';
export default function App() {
const [compact, setCompact] = useState(() => {
try {
return localStorage.getItem('meetcode_compact') === '1';
} catch {
return false;
}
});
useEffect(() => {
try {
localStorage.setItem('meetcode_compact', compact ? '1' : '0');
} catch {}
}, [compact]);
return (
<div style={{ padding: 16 }}>
<h2 style={{ marginTop: 0 }}>meetcode settings</h2>
<label style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
<input type='checkbox' checked={compact} onChange={(e) => setCompact(e.target.checked)} />
Compact mode
</label>
<div style={{ marginTop: 12, border: '1px solid #eee', borderRadius: 16, padding: compact ? 10 : 18 }}>
<strong>Preview card</strong>
<div style={{ color: '#555', marginTop: 8 }}>Padding changes based on setting.</div>
</div>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!