Back to questions
Middle
4
Implement a search and filter functionality in a React component using useState. - Use useState to create a search state and a roleFilter state. - Bind the search state to an input field's value and update it on change. - Bind the roleFilter state to a select dropdown's value and update it on change. - Style the input field with padding and font size.
Companies where asked
RWB
Answer from AI
sobes.tech AI
jsx import React, { useState } from 'react';
function SearchFilterComponent() { const [search, setSearch] = useState(''); const [roleFilter, setRoleFilter] = useState('');
return ( <div> <input type="text" value={search} onChange={e => setSearch(e.target.value)} placeholder="Search..." style={{ padding: '8px', fontSize: '16px' }} />
<select
value={roleFilter}
onChange={e => setRoleFilter(e.target.value)}
>
<option value="">All Roles</option>
<option value="admin">Admin</option>
<option value="user">User</option>
<option value="guest">Guest</option>
</select>
{/* Здесь можно добавить логику отображения отфильтрованных данных */}
</div>
); }
export default SearchFilterComponent;