Trying to filter data using GraphQL variables and Next JS. How can I pass my state to the GraphQL variable "name" ?
handleSelectedFilter will update the filter state based on the selected option. I am expecting the value of the filter state to get passed to "name" variable.
Bare in mind we cannot use react hooks inside of Next js getStaticProps function, tried state management, custom hooks ..etc doesn't work with Next JS.
Any kind of help is much appreciated. Thanks in advance.
export async function getStaticProps() {
const { data, errors } = await client.query({
query: GET_LISTINGS,
variables: {
uri: '/listing/',
name: filter?.city
}
})
return {
props: {
data: data || [],
},
revalidate: 1
}
}
const listings = ({ data }) => {
const [filter, setFilter] = useState({
city: '',
area: '',
type: '',
})
const handleSelectedFilter = e => {
setFilter({ ...filter, [e.target.name]: e.target.value })
}
}
export default listings
GrpahQL Schema below
export const GET_LISTINGS = gql`
query GET_LISTINGS( $name: String) {
listingCategoriesL: listingCategories(where: {name: [$name]}) {
nodes {
id
name
listings {
edges {
node {
id
title
}
}
}
}
}
}