首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next-Auth & React.Component

Next-Auth & React.Component
EN

Stack Overflow用户
提问于 2022-05-17 07:59:34
回答 2查看 205关注 0票数 1

Next有下面的示例,它对函数非常有用,但是我有一个类,需要在其中运行const { data: session } = useSession()。我想知道如何将其转换为在类中有效?

代码语言:javascript
复制
export default function AdminDashboard() {
  const { data: session } = useSession()
  // session is always non-null inside this page, all the way down the React tree.
  return "Some super secret dashboard"
}

AdminDashboard.auth = true

我试图将session: useSession()添加到下面的构造函数中,但是它没有工作。

我的班

代码语言:javascript
复制
export default class AdminDashboard extends React.Component {
 constructor(props) {
        super(props);
        this.state = {
          value: null,
          areas:[],
          areasid:[],
          users: [],
          isLoading: true,
          isAreaLoading: true,
          session: useSession() // THIS DID NOT WORK
        };
        this.checkAnswer = this.checkAnswer.bind(this);
        
      }
}
AdminDashboard.auth = true

根据下面的答案。我把剧本改成这样

代码语言:javascript
复制
const withSession = (Component) => (props) => {
  const session = useSession()

  // if the component has a render property, we are good
  if (Component.prototype.render) {
    return <Component 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")
  )
}



export default class NewCampaign extends React.Component {
render(){
        const { data: session, status } = this.props.session;
        const { isLoading, users, areas, areasid, isAreaLoading } = this.state;
        return (
            <React.Fragment></React.Fragment>
         )}
}

const ClassComponentWithSession = withSession(NewCampaign)

NewCampaign.auth = false;

NewCampaign.getLayout = function getLayout(page) {
  return (
    <Dashboard>
      {page}
    </Dashboard>
  )
}

但是,由于“this.props.session”的属性“数据”是未定义的,因此无法对其进行重构。

EN

回答 2

Stack Overflow用户

发布于 2022-05-17 09:01:39

您应该使用getSession,只需使用等待的结果。

代码语言:javascript
复制
async function myFunction() {
  const session = await getSession()
  // session available here
}

您可以在客户端和服务器上使用它。

票数 2
EN

Stack Overflow用户

发布于 2022-05-17 08:16:22

如果您想在类组件中使用useSession()钩子,您可以在高级组件的帮助下这样做,或者使用渲染工具。

高阶分量

代码语言:javascript
复制
import { useSession } from "next-auth/react"

const withSession = (Component) => (props) => {
  const session = useSession()

  // if the component has a render property, we are good
  if (Component.prototype.render) {
    return <Component 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 { data: session, status } = this.props.session
    return null
  }
}

const ClassComponentWithSession = withSession(ClassComponent)

渲染支撑

代码语言:javascript
复制
import { useSession } from "next-auth/react"

const UseSession = ({ children }) => {
  const session = useSession()
  return children(session)
}

// Usage
class ClassComponent extends React.Component {
  render() {
    return (
      <UseSession>
        {(session) => <pre>{JSON.stringify(session, null, 2)}</pre>}
      </UseSession>
    )
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72270208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档