Is there any way to use NextAuth in class based component? #840
Answered
by
balazsorban44
MateuszWawrzynski
asked this question in
Help
-
Your question What are you trying to do Feedback
|
Beta Was this translation helpful? Give feedback.
Answered by
balazsorban44
Nov 10, 2020
Replies: 1 comment 8 replies
-
You could create a HOC (Higher Order Component) that sends them as props to your class component: import {useSession} from "next-auth/client"
const withSession = ClassComponent => props => {
const session = useSession()
if(ClassComponent.prototype.render) { // if the component has a render property, we are good
return <ClassComponent session={session} {...props}/>
}
// if the passed component is a Function Component, there is no need for this wrapper
throw new Error([
"You passed a function component, `withSession` is not needed.",
"You can `useSession` directly in your component."
].join("\n"))
}
// Usage
class ClassComponent extends React.Component {
render() {
const {session} = this.props
return null
}
}
const FuncComponent = ({session}) => {
const session = useSession() // use this
return null
}
const ClassComponentWithSession = withSession(ClassComponent)
const FuncComponentWithSession = withSession(FuncComponent) // will throw error, as this is not needed |
Beta Was this translation helpful? Give feedback.
8 replies
Answer selected by
balazsorban44
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could create a HOC (Higher Order Component) that sends them as props to your class component: