This is the assignment of our university. I tried to add the useEffect to improve my small toy-project to have searchFilter but it says
TypeError: Cannot read property 'filter' of undefined
on useEffect/setSearchFilter part
This was the previous situation of this problem. I feel disaster for this problem holding this for more than 2 days
/[Error image][1]
Can anyone help me with this?
import React, { useState, useEffect } from 'react'
import './App.css';
function App() {
const [datas, setDatas] = useState([]);
const [userid, setUserid] = useState('');
const [searchFilter, setSearchFilter] = useState('');
const inputChanged = (event) => {
setUserid(event.target.value)
}
useEffect(() => {
fetch('https://api.github.com/search/repositories?q=react')
.then(response => response.json())
.then(data => {
setDatas(data.items)
})
},[])
useEffect(() => {
setSearchFilter(
datas.full_name.filter((result) =>
result.name.toLowerCase().includes(userid.toLowerCase())
)
);
}, [userid, searchFilter]);
return (
<div className="App">
<h1>Repositories</h1>
<input
id="searchInput"
type="text"
placeholder="search"
name="search"
value={userid}
onChange={inputChanged}
/>
<button onClick={(e) => setSearchFilter(e.target.value)}>Search</button>
<table>
<tbody>
<tr>
<th>Name</th>
<th>URL</th>
</tr>
{datas.map((data, index) =>
<tr key={index}>
<td>{data.full_name}</td>
<td><a
target="_blank"
href={data.html_url}
>
{data.html_url}
</a>
</td></tr>
)}
</tbody>
</table>
</div>
);
}
export default App;```
https://stackoverflow.com/questions/67761971/searchfilter-with-using-react-hookuseeffect-usestate/67762090#67762090
[1]: https://i.stack.imgur.com/RuubM.png