I’ve now implemented this and added useState to update the skip variable in my graphQL query along with some buttons.
However, one strange thing! … clicking the buttons does not update the news results unless you move away from the browser window and then go back again, then the content updates. Have I done something wrong here…
import React, { useState } from 'react'
import { useQuery } from 'react-query'
import gql from 'graphql-tag'
import { GraphQLClient } from 'graphql-request'
export const useGQLQuery = (key, query, variables) => {
const endpoint = `https://graphql.datocms.com/`
const client = new GraphQLClient(endpoint, {
headers: {
authorization: `Bearer MY_TOKEN_HERE`,
}
});
return useQuery(key, () => client.request(query, variables)); // useQuery from react-query
};
const GET_ALL_NEWS_CONTENT = gql`
query GetAllNewsContent ($first: IntType = 10, $skip: IntType = 0) {
allNews(first: $first, skip: $skip, orderBy: _createdAt_DESC) {
id
title
slug
}
}
`;
export default function AllNews() {
const first = 5
let [skip, skipNews] = useState(0)
// Fetch data from custom hook that uses React-Query
const { data, isLoading, error } = useGQLQuery(
'allNews',
GET_ALL_NEWS_CONTENT,
{ skip, first }
);
// console.log(data)
if (isLoading) return <div>Loading…</div>
if (error) return <div>Something went wrong…</div>
return (
<>
<div className="generic-page">
<button onClick={() => skipNews(0)}>page 1</button>
<button onClick={() => skipNews(5)}>page 2</button>
<button onClick={() => skipNews(10)}>page 3</button>
<button onClick={() => skipNews(15)}>page 4</button>
<h2>All News</h2>
<div>
{data.allNews.map(news => (
<div key={news.title}>{news.title}</div>
))}
</div>
</div>
</>
)
};
@steven I’m not particularly familiar with react-query, but everything tells me this is not related to DatoCMS I’d suggest asking directly to the react-query community, you’ll have better chances to get expenceried help in this regard!
hey @steven I’m sorry but the secret management is again something that is managed by your project, it’s difficult for us to help you there unless we have access to your repo
I hope you understand. If you cannot figure that out yourself feel free to DM me so that you can maybe add me to your repo and I can help you out, OK?