My app returns results via API when a search is performed. For each result there is a detail link. Clicking on the link and going back what I searched for is not saved, the search bar is empty. You have to rewrite the previous search to select another result. I would like the general search to be saved when going back.
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>
I'm using the .map function to filter the results and the react-router link to open the "details" page.
{result.map(book => ( ------
<Link className="btn btn-primary mt-auto"
to={
pathname: '/details',
}>
View
</Link>