Skip to content

Is there any way to use NextAuth in class based component? #840

Discussion options

You must be logged in to vote

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()

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@balazsorban44
Comment options

@krisztianzagonyi-mintestio
Comment options

@balazsorban44
Comment options

@krisztianzagonyi-mintestio
Comment options

@balazsorban44
Comment options

Answer selected by balazsorban44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #840 on December 07, 2020 16:54.