ReactJS Program to Dependent Select Inputs with Explanation
ReactJS
Hard
Forms & Validation
30 views
1 min read
84 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around select, second, first. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create two selects where second options depend on first selection.
Input Format
No input.
Output Format
Render a React component.
Constraints
Reset second value when first changes.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useMemo, useState } from 'react';
const data = {
Frontend: ['HTML', 'CSS', 'React'],
Backend: ['PHP', 'Node', 'Python']
};
export default function App() {
const [track, setTrack] = useState('Frontend');
const [topic, setTopic] = useState('HTML');
const topics = useMemo(() => data[track], [track]);
function changeTrack(next) {
setTrack(next);
setTopic(data[next][0]);
}
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode chooser</h2>
<div style={{ display: 'grid', gap: 10 }}>
<select value={track} onChange={(e) => changeTrack(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }}>
<option>Frontend</option>
<option>Backend</option>
</select>
<select value={topic} onChange={(e) => setTopic(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }}>
{topics.map((t) => <option key={t}>{t}</option>)}
</select>
</div>
<p style={{ marginTop: 12, color: '#555' }}>Selected: {track} → {topic}</p>
</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 two selects where second options depend on first selection.
Input / Output
Output
Render a React component.
Constraints
Reset second value when first 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, { useMemo, useState } from 'react';
const data = {
Frontend: ['HTML', 'CSS', 'React'],
Backend: ['PHP', 'Node', 'Python']
};
export default function App() {
const [track, setTrack] = useState('Frontend');
const [topic, setTopic] = useState('HTML');
const topics = useMemo(() => data[track], [track]);
function changeTrack(next) {
setTrack(next);
setTopic(data[next][0]);
}
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode chooser</h2>
<div style={{ display: 'grid', gap: 10 }}>
<select value={track} onChange={(e) => changeTrack(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }}>
<option>Frontend</option>
<option>Backend</option>
</select>
<select value={topic} onChange={(e) => setTopic(e.target.value)} style={{ padding: '10px 12px', borderRadius: 12, border: '1px solid #bbb' }}>
{topics.map((t) => <option key={t}>{t}</option>)}
</select>
</div>
<p style={{ marginTop: 12, color: '#555' }}>Selected: {track} → {topic}</p>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!