React useOktaAuth is undefined

Hi, I’m having trouble getting the okta-react library to work for me. when I try to access useOktaAuth() I get undefined returned. I’m following this tutorial: https://developer.okta.com/docs/guides/sign-into-spa/react/add-signin-button/
Most basic setup that should work:

import React, { useState, useEffect } from "react"
import { useOktaAuth } from "@okta/okta-react"
const AccountPage = () => {
  console.log(useOktaAuth()) // => undefined
  const { authState, authService } = useOktaAuth() // => TypeError: Cannot read property 'authState' of undefined
}

What am I missing here?

1 Like

Solved: The code should be wrapped around a BrowserRouter (from react-router-dom) and Security (@okta/okta-react) element.

1 Like