github api 的用户发现项目?
User discovery project with github api?
我正在开发一个项目,使用 github api 按用户名查找用户,但是当我输入用户名时,我无法得到任何结果。
我的项目旨在从 github 中获取搜索到的用户的用户信息。
我找不到错误我已经尝试了所有方法。
我的代码:
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"
]
}
}
哪里错了?
抓取有问题吗api?
你能帮帮我吗
由于您使用 HTML 表单的 onSubmit
方法,表单执行的默认任务是 POST 对您的 URL 的请求。为防止这种情况发生,您需要对 handleSubmit
方法进行以下更改。
const handleSubmit = (e) => {
e.preventDefault();
fetch(`https://api.github.com/users/${userInput}`)
.then(res => res.json())
.then(data => {
setData(data);
})
}
有关 API 的更多详细信息,请参见此处:https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
我正在开发一个项目,使用 github api 按用户名查找用户,但是当我输入用户名时,我无法得到任何结果。 我的项目旨在从 github 中获取搜索到的用户的用户信息。 我找不到错误我已经尝试了所有方法。
我的代码:
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"
]
}
}
哪里错了?
抓取有问题吗api?
你能帮帮我吗
由于您使用 HTML 表单的 onSubmit
方法,表单执行的默认任务是 POST 对您的 URL 的请求。为防止这种情况发生,您需要对 handleSubmit
方法进行以下更改。
const handleSubmit = (e) => {
e.preventDefault();
fetch(`https://api.github.com/users/${userInput}`)
.then(res => res.json())
.then(data => {
setData(data);
})
}
有关 API 的更多详细信息,请参见此处:https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault