-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Two custom restoreOriginalUri callbacks are detected" warning #227
Comments
Same issue since upgrading to React 18 |
@CruseCtrl What are the reasons of re-rendering component? |
@denysoblohin-okta every time anything on the page changes, React re-renders the components. I'm not sure I fully understand the question |
Security component should re-render and call its useEffect hook if |
I'm not changing Edit: To be clear, the |
Unlike It's safe to remove dependency from
Some explanation of why it's safe: |
Internal ref: OKTA-500147 |
Hi, even though I've added a useCallback to restoreOriginalUri, and removed navigate from dependencies, the warning in the console still crops up. Do you have any ideas why the recommendations don't work? |
I fixed it by disabling React strict mode. (Remove <React.StrictMode> from index.js) Strict mode will intentionally render every component twice. |
I noticed the same thing as vdzerakh mentioned above. Even with the I think the problem here is that Strict Mode enforces double renders in development so that you can catch side effects.
This useEffect triggers the first time, and the warning does not trigger, since the condition isn't true yet. But then on the second render (the one from Strict Mode), it triggers because the condition I'm still thinking about alternative approaches to this. Follow up I did some rudimentary testing, and I came up with this:
This is a clean up function from the return of the useEffect. Whenever the |
Temp solution I'm using in the meantime: import { Security } from '@okta/okta-react'
import { useCallback, useEffect, useRef } from 'react'
import { useNavigate } from 'react-router-dom'
import { toRelativeUrl } from '@okta/okta-auth-js'
export default function Root() {
const navigate = useNavigate()
const navigateRef = useRef(navigate)
// Allow for "stale" navigate references since originalUri will be an absolute URL.
const restoreOriginalUri = useCallback((_, originalUri = '/') => {
const url = toRelativeUrl(originalUri, globalThis.location.origin)
navigateRef.current(url)
}, [])
useEffect(() => {
return () => {
oktaAuth.options.restoreOriginalUri = undefined
}
}, [])
return (
<Security oktaAuth={oktaAuth} restoreOriginalUri={restoreOriginalUri}>
...
</Security>
)
} |
#239 seems to fix this. |
If I remove the above fix: useEffect(() => {
return () => {
oktaAuth.options.restoreOriginalUri = undefined
}
}, []) I'm still seeing this error with
I've also updated my "SecureRoute" to reflect the export default function SecureRoute() {
const { oktaAuth, authState } = useOktaAuth()
useEffect(() => {
if (authState?.isAuthenticated === false) {
const originalUri = toRelativeUrl(
window.location.href,
window.location.origin,
)
oktaAuth.setOriginalUri(originalUri)
oktaAuth.signInWithRedirect()
}
}, [oktaAuth, authState?.isAuthenticated])
if (authState?.isAuthenticated === true) {
return <Outlet />
} else {
return <FullPageLoading />
}
} |
@kellengreen @ryanmr - Thanks for the solutions. |
@kellengreen / @ryanmr , do you have any idea whats need to be done in the react app when we doing the single sign out from the okta dashboard |
@phani1585 Can you please create a new issue, your question doesn't seem relevant to this thread |
@phani1585 sorry I'm not sure if I understand your question. For what it's worth, I don't recall seeing this warning in recent releases. |
Describe the bug?
I'm getting a warning saying that "Two custom restoreOriginalUri callbacks are detected. The one from the OktaAuth configuration will be overridden by the provided restoreOriginalUri prop from the Security component."
What is expected to happen?
I shouldn't get a warning
What is the actual behavior?
I get a warning
Reproduction Steps?
SDK Versions
Execution Environment
MacOS Big Sur 11.6
Additional Information?
It looks like this is caused in
Security.tsx
on lines 51-56.In the first render,
oktaAuth.options.restoreOriginalUri
won't exist and so it won't log a warning. But then on line 54 it sets a value tooktaAuth.options.restoreOriginalUri
. So on the next render when it checksif (oktaAuth.options.restoreOriginalUri && restoreOriginalUri)
it will show the warning, because it's just added a value! (And theuseEffect
will run again because you've just changed one of the properties ofoktaAuth
.)This problem is also mentioned in #170, where the accepted solution was to create a new
oktaAuth
for each render. This will get rid of the error because it doesn't reuse theoktaAuth
which now has arestoreOriginalUri
, but it's bad practice to create a new object on every render.It's a bit weird that you're modifying the
oktaAuth
object that's passed in. Some options of how to fix this are:oktaAuth
and modify the copy, leaving the original unchangedrestoreOriginalUri
into theOktaContext.Provider
so that we can keep the originaloktaAuth
restoreOriginalUri
as a separate parameter toSecurity
, instead requiring that the user sets therestoreOriginalUri
directly on theoktaAuth.options
themselves (and add a suitable error message if the user hasn't done this)I think 3 is my favourite, but it would require a breaking change so I'd welcome any feedback. I'm also happy to raise a pull request once we've agreed upon a solution
The text was updated successfully, but these errors were encountered: