Hello
Using auth0-spa-js 1.0.2
, react 16.8.6
I am trying to set up Auth0 for an SPA and it works amazing for functional components in React.
I am however stuck when I try to make an API call in a Class Component.
This is what I use in my functional components:
import React from 'react';
import { useAuth0 } from '../react-auth0-spa';
...
const Profile = () => {
const { loading, user, getTokenSilently } = useAuth0();
...
And this is what I tried to do in a class component:
import React from 'react';
import { useAuth0 } from '../react-auth0-spa';
...
class Admin extends React.Component {
...
async componentDidMount() {
const { loading, user, getTokenSilently } = useAuth0();
try {
const token = await getTokenSilently();
...
Which results in the following error:
Uncaught (in promise) Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
… with references to the const { loading, user, getTokenSilently } = useAuth0();
line and const token = await getTokenSilently();
.
I assume this is simply because I don’t understand how to use the getTokenSilently function inside a class component, but I cannot find any documentation or guides on it, and I have been (sadly) trying to fix this same error for hours. I hope someone can give me a hand or point me in the right direction.
Thanks in advance.