【发布时间】:2021-06-17 09:08:20
【问题描述】:
执行搜索时,我的应用会通过 API 返回结果。每个结果都有一个详细链接。单击链接并返回我搜索的内容未保存,搜索栏为空。您必须重写先前的搜索以选择另一个结果。我希望在返回时保存常规搜索。
function GoogleBooksSearch() {
const [book, setBook] = useState("");
const [result, setResult] = useState([]);
const apiKey = ("MY API")
function handleChange(event) {
const book = event.target.value;
console.log(event.target.value);
setBook(book);
}
function handleSubmit(event) {
event.preventDefault();
axios.get("https://www.googleapis.com/books/v1/volumes?q=" + book + "&key=" + apiKey + "&maxResults=20")
.then(data => {
console.log(data.data.items);
setResult(data.data.items);
})
}
return (
<div>
<div className="hero-container">
<h1>Search your book</h1>
<p>Discover the best books ever</p>
<form onSubmit={handleSubmit}>
<div className='container'>
<div>
<input onChange={handleChange} className="form" autoFocus placeholder="Enter the title of the book" type="text" />
<div style={{ marginTop: '20px' }}></div>
<div className='d-grid gap-2 '>
<input type="submit" value="SEARCH" className="btn btn-primary btn-lg mx-auto" />
</div>
</div>
</div>
</form>
</div>
我正在使用 .map 函数过滤结果,并使用 react-router 链接打开“详细信息”页面。
{result.map(book => ( ------
<Link className="btn btn-primary mt-auto"
to={
pathname: '/details',
}>
View
</Link>
【问题讨论】:
标签: reactjs search react-router react-hooks