Fetch data from GraphQL endpoint onCLick

about 2 years ago - Direct link

I'm using Apollo Client / mst-gql. The problem is that when I click my button for the first time I get undefined. I had the same issue before (when the page loaded for the first time), but I was able to fix it using custom hooks.

const useAccountData = () => {

const [data, setData] = useState();
useEffect(() => {
const fetchData = async () => {
const { customerData } = await store.requestAccount();
return getProductById;
};
fetchImages().then(setData);
}, []);
return data;
};

The problem now, is that I'm not allowed to use hooks inside of an onCLick. So I get this error:

React Hook "useAccountData" is called in function "onClick" which is neither a 

React function component or a custom React Hook function


Go to article →