I'm having trouble figuring out how to pass data from a component to getServerSideProps, basically what I want is to get the value of text input and use it as a keyword in my API call. The text input will be located in a "SearchBar" component that will be visible on every page of my application and on button click I want to make the API call using the value of the input in a proper page called "SearchResults". What is the best way to do that?
SearchBar:
import { useState } from 'react';
export default function SearchBar() {
const [keyword, setKeyword] = useState('');
const handleClick = (event) => {
event.preventDefault();
console.log(keyword);
};
return (
<div>
<input
type="text"
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
/>
<button onClick={handleClick}>Fetch</button>
</div>
);
}
SearchResults:
import { GetServerSideProps } from 'next';
export const getServerSideProps: GetServerSideProps = async (context) => {
const { keyword } = context;
const res = await fetch(
`https://www.googleapis.com/books/v1/volumes?q=${keyword}`
);
const data = await res.json();
return { props: {} };
};
export default function SearchResults() {
return (
<div>
<h1>Teste</h1>
</div>
);
}