ReactJS Program to Toggle Multiple Filters with Explanation
ReactJS
Medium
State Management
33 views
1 min read
89 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around filter, toggle, react. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Make three filter chips (Easy, Medium, Hard) that can be toggled.
Input Format
No input.
Output Format
Render a React component.
Constraints
Store selected filters in an array and toggle by value.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useState } from 'react';
const levels = ['Easy', 'Medium', 'Hard'];
function Chip({ active, children, onClick }) {
return (
<button
type='button'
onClick={onClick}
style={{ padding: '8px 12px', borderRadius: 999, border: '1px solid ' + (active ? '#0b5' : '#ddd'), background: active ? '#eafaf1' : '#fff', color: '#124', fontWeight: 600 }}
>
{children}
</button>
);
}
export default function App() {
const [selected, setSelected] = useState(['Easy']);
function toggle(level) {
setSelected((prev) => (prev.includes(level) ? prev.filter((x) => x !== level) : [...prev, level]));
}
return (
<div style={{ padding: 16 }}>
<strong>meetcode filters</strong>
<div style={{ display: 'flex', gap: 10, marginTop: 10 }}>
{levels.map((l) => (
<Chip key={l} active={selected.includes(l)} onClick={() => toggle(l)}>
{l}
</Chip>
))}
</div>
<p style={{ marginTop: 12, color: '#555' }}>Selected: {selected.length ? selected.join(', ') : 'None'}</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
Make three filter chips (Easy, Medium, Hard) that can be toggled.
Input / Output
Output
Render a React component.
Constraints
Store selected filters in an array and toggle by value.
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).
Difficulty
Medium
ReactJS
Official Solution
import React, { useState } from 'react';
const levels = ['Easy', 'Medium', 'Hard'];
function Chip({ active, children, onClick }) {
return (
<button
type='button'
onClick={onClick}
style={{ padding: '8px 12px', borderRadius: 999, border: '1px solid ' + (active ? '#0b5' : '#ddd'), background: active ? '#eafaf1' : '#fff', color: '#124', fontWeight: 600 }}
>
{children}
</button>
);
}
export default function App() {
const [selected, setSelected] = useState(['Easy']);
function toggle(level) {
setSelected((prev) => (prev.includes(level) ? prev.filter((x) => x !== level) : [...prev, level]));
}
return (
<div style={{ padding: 16 }}>
<strong>meetcode filters</strong>
<div style={{ display: 'flex', gap: 10, marginTop: 10 }}>
{levels.map((l) => (
<Chip key={l} active={selected.includes(l)} onClick={() => toggle(l)}>
{l}
</Chip>
))}
</div>
<p style={{ marginTop: 12, color: '#555' }}>Selected: {selected.length ? selected.join(', ') : 'None'}</p>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!