ReactJS Program to Icon Only Button Accessibility with Explanation
ReactJS
Medium
Component Design
25 views
1 min read
86 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around icon, button, accessibility. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Create an icon-only button that is accessible using aria-label.
Input Format
No input.
Output Format
Render a React component.
Constraints
Do not rely on icon text for accessibility.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React from 'react';
export function IconButton({ label, onClick }) {
return (
<button
type='button'
aria-label={label}
onClick={onClick}
style={{ width: 40, height: 40, borderRadius: 12, border: '1px solid #ddd', background: '#fff', display: 'grid', placeItems: 'center' }}
>
<span aria-hidden='true' style={{ fontSize: 18 }}>≡</span>
</button>
);
}
export default function App() {
return <IconButton label='Open menu' onClick={() => {}} />;
}
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 an icon-only button that is accessible using aria-label.
Input / Output
Output
Render a React component.
Constraints
Do not rely on icon text for accessibility.
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 from 'react';
export function IconButton({ label, onClick }) {
return (
<button
type='button'
aria-label={label}
onClick={onClick}
style={{ width: 40, height: 40, borderRadius: 12, border: '1px solid #ddd', background: '#fff', display: 'grid', placeItems: 'center' }}
>
<span aria-hidden='true' style={{ fontSize: 18 }}>≡</span>
</button>
);
}
export default function App() {
return <IconButton label='Open menu' onClick={() => {}} />;
}
Solutions (0)
No solutions submitted yet. Be the first!