I can't find how to filter a table in React with the Select (dropdown). I found an example on Google but they used Node but their code doesn't work on my React app. I prefer using components not class functions. This is my code. And I want to filter the table exemple by select options?:
const [data, getData] = useState([])
const URL = 'https://jsonplaceholder.typicode.com/posts'
useEffect(() => {
fetchData()
}, [])
const fetchData = () => {
fetch(URL)
.then((res) =>
res.json())
.then((response) => {
console.log(response);
getData(response);
})
}
return (
<>
<Select options=["A","B","C"] />
<table>
<tr>
<th>User Id</th>
<th>Id</th>
<th>Title</th>
<th>Description</th>
</tr>
{data.map((item, i) => (
<tr key={i}>
<td>{item.userId}</td>
<td>{item.id}</td>
<td>{item.title}</td>
<td>{item.body}</td>
</tr>
</table>
</>
In short, in order to filter by selected option you have to:
Here is the working code:
Working Sandbox: https://codesandbox.io/s/wonderful-feistel-qgrm0f?file=/src/App.js