【问题标题】:How to get params id in react class component如何在反应类组件中获取参数ID
【发布时间】:2021-04-25 22:17:34
【问题描述】:

我想从这个类组件的 url 中得到一个参数 id

App.js

<Switch>
    <Route exact path="/products/:id">
       <AuthChecker>
          <Products />
       </AuthChecker>
    </Route>
</Switch>
    

Products.js

import React, { Component } from 'react'
    
    class Products extends Component {

     //How to get params id in this class component

      render() {
        return (
          <div>
            <h5></h5>
          </div>
        )
      }
    }

   export default Products
            

我确实检查了一些问题,但我没有得到类组件

提前致谢

【问题讨论】:

  • 你检查类构造函数了吗?
  • 我尝试了 match 但它不起作用但现在它通过传递 props match 来工作

标签: reactjs class url parameters react-class-based-component


【解决方案1】:

match.paramsRoute's 渲染道具结合使用:

<Switch>
    <Route exact path="/products/:id" render={({ match }) => (
       <AuthChecker>
          <Products id={match.params.id} />
       </AuthChecker>
    )} />
</Switch>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2016-12-20
    • 2017-11-12
    • 2021-12-02
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多