I'm trying to expose the data from a React.js hook as a REST endpoint using Next.js.
I can easily create a REST endpoint using Next.js using the following code at pages/api/index.tsx
export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ hello: 'World' });
}
However, when I attempted to return the data from a React.js hook (fetching the data from an external API), I received an error that I can't access React.js hooks outside an React function or custom hook.
Is there any work-around for this? Or should I do something different with the hook in order to be able to retrieve the data in this file?
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const snxjs = useContext(SNXJSContext);
const {
SNXPrice,
SNXTotalSupply,
SNXPercentLocked,
issuanceRatio,
activeCRatio,
totalIssuedSynths,
SNXPriceQuery,
SNXTotalSupplyQuery,
issuanceRatioQuery,
totalIssuedSynthsQuery,
SNXHoldersQuery,
} = useSNXInfo(snxjs);
res.status(200).json({ SNXPrice: SNXPrice });
}