Created
November 3, 2021 12:01
-
-
Save aidiary/7085c2529b0cd72f5d604bab7a19feaa to your computer and use it in GitHub Desktop.
[React] テキストボックスに入力して絞り込む機能
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Fragment, useEffect, useState } from 'react'; | |
import classes from './UserFinder.module.css'; | |
import Users from './Users'; | |
const DUMMY_USERS = [ | |
{ id: 'u1', name: 'Max' }, | |
{ id: 'u2', name: 'Manuel' }, | |
{ id: 'u3', name: 'Julie' }, | |
]; | |
const UserFinder = () => { | |
const [filteredUsers, setFilteredUsers] = useState(DUMMY_USERS); | |
const [searchTerm, setSearchTerm] = useState(''); | |
useEffect(() => { | |
setFilteredUsers( | |
DUMMY_USERS.filter((user) => user.name.includes(searchTerm)) | |
); | |
}, [searchTerm]); | |
const searchChangeHandler = (event) => { | |
setSearchTerm(event.target.value); | |
}; | |
return ( | |
<Fragment> | |
<div className={classes.finder}> | |
<input type='search' onChange={searchChangeHandler} /> | |
</div> | |
<Users users={filteredUsers} /> | |
</Fragment> | |
); | |
}; | |
export default UserFinder; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment