MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

ReactJS Program to Accessible Error Messages with Explanation

ReactJS Medium Forms & Validation 22 views
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around error, react, accessible. Let’s break it down step by step so you can implement it confidently.
Back to Questions

Problem Statement

Build a field that links error text using aria-describedby.

Input Format

No input.

Output Format

Render a React component.

Constraints

Give error element an id and attach it to input when error exists.

Code Solution

This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useId, useState } from 'react'; export default function App() { const [value, setValue] = useState(''); const [touched, setTouched] = useState(false); const errId = useId(); const error = touched && value.trim().length < 3 ? 'Enter at least 3 characters' : ''; return ( <div style={{ padding: 16, width: 520 }}> <h2 style={{ marginTop: 0 }}>meetcode accessibility</h2> <label style={{ fontWeight: 700 }}>Display name</label> <input value={value} onChange={(e) => setValue(e.target.value)} onBlur={() => setTouched(true)} aria-invalid={!!error} aria-describedby={error ? errId : undefined} style={{ marginTop: 6, width: '100%', padding: '10px 12px', borderRadius: 12, border: '1px solid ' + (error ? '#c1121f' : '#bbb') }} /> {error ? <div id={errId} style={{ marginTop: 8, color: '#c1121f' }}>{error}</div> : <div style={{ marginTop: 8, color: '#555' }}>Looks good.</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).

Notes & Extra Practice

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next