I'm developing a project to find users by username using github api but when i type the username i can't get any result. my project aims to fetch the user information of the searched user from github. I can't find the error I've tried everything.
My codes:
import React,{useState, useEffect} from "react";
import { BiUser } from 'react-icons/bi';
import './App.css';
function App() {
const [name, setName] = useState("")
const [userName, setUserName] = useState("")
const [followers, setFollowers] = useState("")
const [following, setFollowing] = useState("")
const [repos, setRepos] = useState("")
const [avatar, setAvatar] = useState("")
const [userInput, setUserInput] = useState("")
const [error, setError] = useState(null)
useEffect(()=> {
fetch("https://api.github.com/users/example")
.then(res => res.json())
.then(data => {
setData(data)
})
}, [])
const setData = ({name, login, followers, following, public_repos, avatar_url}) => {
setName(name);
setUserName(login);
setFollowers(followers);
setFollowing(following);
setRepos(public_repos);
setAvatar(avatar_url)
}
const handleSearch = (e) => {
setUserInput(e.target.value)
}
const handleSubmit = () => {
fetch(`https://api.github.com/users/${userInput}`)
.then(res => res.json())
.then(data => {
setData(data);
})
}
return (
<div>
<div className="navbar">
Github Search
</div>
<div className="search">
<form onSubmit={handleSubmit}>
<input placeholder="Github user" name="github user" onChange={handleSearch}></input>
<button>Search</button>
</form>
</div>
<div className="card">
<div className="card-alt">
<div className="image">
<img src={avatar} alt="img" width="270px" height="270px"/>
</div>
<div className="card-content">
<h3> {name}</h3>
<h3>{userName}</h3>
</div>
<div className="card-content">
<a>
<BiUser className="icon"/>
{followers} Followers
</a>
</div>
<div className="card-content">
<a>
<BiUser className="icon"/>
{repos} Repos
</a>
</div>
<div className="card-content end">
<a>
<BiUser className="icon"/>
{following} Following
</a>
</div>
</div>
</div>
</div>
);
}
export default App;
package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Where is it wrong?
Is there a problem with fetch api?
can you help me
CodePudding user response:
Since you use the HTML form's onSubmit
method, the default task that a form performs is POST that request to your URL. To prevent this from happening, you need to make the following change to your handleSubmit
method.
const handleSubmit = (e) => {
e.preventDefault();
fetch(`https://api.github.com/users/${userInput}`)
.then(res => res.json())
.then(data => {
setData(data);
})
}
More details about the API can be found here : https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault