【问题标题】:nextjs pass current route to the higher order cmponent as parametrenextjs 将当前路由作为参数传递给高阶组件
【发布时间】:2020-06-21 19:50:05
【问题描述】:

我有一个 hoc withAuth.js 我在其中传递一个组件,第二个参数是当前路由,如果条件失败,它将呈现

const Detail = (props) => {
    return(
        <>
        <div> this is the my account inside  ..... </div>
        </>
    )
};

export async function getServerSideProps({req ,res}) {
    // Call an external API endpoint to get posts.
    // You can use any data fetching library
   
    // console.log('request object ', req.headers);
    
    // retun the props.api bcoz client side has no access to the baseurl from the server 
    return {
        props:{}
    }
  }

export default withAuth(Detail, loginRedirectPath);

我的问题是如何将当前路由传递给 hoc

编辑 嗨,我已经通过管理路线历史解决了这个问题

【问题讨论】:

  • “当前路由”是指像example.com/sth这样的整个URL吗?
  • @Hangindev 是 url 或路径

标签: reactjs next.js


【解决方案1】:

我认为您实际上不需要将其作为参数传递,但如果您愿意,您可以这样做。根据docs,您可以使用 useRouter() 挂钩(或其他方法,例如导入 Router 对象)来获取当前路径名。我相信这在使用钩子时对组件或 HOC 都有效,尽管我可能错了。无论如何,使用 next/router 来获取路径名是这里的方法!

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2020-02-06
    • 2019-05-18
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多