I'm learning React and making a very simple app, where I am getting info from github api and displaying my repos information. It doesn't work tho and I keep getting an error when I'm trying to find a particular
Uncaught TypeError: data.find is not a function
Here's my code (I deleted almost everything meaningful and left only the part where I'm trying to get at least one repository from the array):
`const RepoList = ({ repos }) => {
const data = Getapiinfo("egor-no");
const repo = data.find(item => item.name == "clangametesttask")
return (
<>
<p>{JSON.stringify(repo)}</p>
</>
);
}`
That's what Getapiinfo function does (gets all the information about all my repos):
function Getapiinfo(login) {
const [data, setData] = useState(null);
useEffect(() => {
fetch(`https://api.github.com/users/${login}/repos`)
.then((response) => response.json())
.then(setData);
}, []);
if (data) {
return data;
};
return "";
}
I'm not sure what I am missing, because data
is definitely an array. At least when I delete the find line, it all works as expected and gets an element by its index:
const RepoList = ({ repos }) => {
const data = Getapiinfo("egor-no");
return (
<>
<p>{JSON.stringify(data)}</p>
<p>{JSON.stringify(data[0])}</p>
</>
);
}
Can you help me with that? I've already spent too much time on this problem and I know it is something simple that I can't just notice. I've also looked through several topics here but none of them seem to have a correct answer or any answers at all :D