How to parse a query string in React Router

后端 未结 3 1739
不思量自难忘°
不思量自难忘° 2021-02-20 08:39

Hello I´m using react router and I need to pass some querystring parameters

tried with



        
3条回答
  •  温柔的废话
    2021-02-20 09:16

    Older versions of React Router offered this functionality but ultimately they decided it was too cumbersome to handle the variation across browsers.

    As of the current version (v4), you need to use a library, such as query-string.

    One more option to consider: if you know your target browsers support the URLSearchParams API, you can use that instead.

    Install package:

    yarn add query-string

    Usage:

    import queryString from 'query-string'
    
    ...
    
    componentDidMount() {
      const values = queryString.parse(this.props.location.search)
    }
    

提交回复
热议问题