reactjs - React Router v4 多个查询字符串

标签 reactjs redux react-router

如何附加多个查询字符串?我无法在任何地方找到 React Router v4 的答案。

现在我只能得到 www.example.com/products?sort=newestwww.example.com/products?page=1

我想要得到的是类似www.example.com/products?sort=newest&page=1

提前谢谢

class CategoriesChild extends React.Component {
  componentWillMount() {
    const values = queryString.parse(this.props.location.search)
    this.props.startSetProductsCategoryChildren(this.props.match.params.category, values.page, values.sort)
  }

  renderProducts () {
    const { props } = this;
      return (
                <h2>{this.props.categoryName}</h2>
                <p>You can sort and filter on the section to the right.</p>
              </div>

              <div className="profile-options-content">
                {
                  this.props.products.length === 0 ?
                  <p style={{textAlign: 'center', opacity: '0.5', marginTop: '50px'}}><i>There is no products in this category just yet!</i></p> :
                  this.props.products.map((product) => {
                    return <ProductListItemMore key={product._id} {...product} />
                  })
                }
                <div className="pagination" >
                  <div className="pagination-bar">
                    {
                      this.props.products.length === 0 ?
                      '' :
                      Array(this.props.pages).fill(0).map((e,i) => {
                        return <Link to={{
                          pathname: `${this.props.match.url}`,
                          search: `&page=${i+1}`
                        }} key={i+1} >{i+1}</Link>
                      })
                    }
                  </div>
                  </div>
              </div>
            </div>     

              <div className="profile-settings-container">
                <div className="profile-settings-container-sort">
                  <h3>Sort</h3>
                  <Link to={{
                    pathname: `${this.props.match.url}`,
                    search: '?sort=newest'
                  }} className="profile-link-button">Newest</Link>
                  <Link to={{
                    pathname: `${this.props.match.url}`,
                    search: '?sort=oldest'
                  }} className="profile-link-button">Oldest</Link>
                  <Link to={{
                    pathname: `${this.props.match.url}`,
                    search: '?sort=price-high'
                  }} className="profile-link-button">Highest Price</Link>
                  <Link to={{
                    pathname: `${this.props.match.url}`,
                    search: '?sort=price-low'
                  }} className="profile-link-button">Lowest Price</Link>
                </div>
              </div>                
      )
    }

主要发布代码...我不知道还要写什么...我添加了所有详细信息...

最佳答案

您只需自己构造它:search: '?sort=price-high&page=1'

如果您有多个查询参数,则可以使用 query-string库,使其更容易/更干净:

import queryString from 'query-string';
...
{ search: queryString.stringify({ sort: 'price-high', page: 1 }) }

关于reactjs - React Router v4 多个查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071949/

相关文章:

javascript - react 未捕获的 DOMException : Failed to execute 'createElement' on 'Document' : The tag name provided ('<div>' ) is not a valid name

javascript - 在 Redux/React 中设置/清除输入

redux - 在 redux 中对同一个数据片进行操作的拆分 reducer

javascript - 内存路由器类型错误 : Cannot read property 'pathname' of undefined

react-router - GraphQL 与 React Router Relay : Expected type Int, 找到数字路由的字符串

reactjs - 如何在 react 中增加和减少状态值?

javascript - 如何让 IntelliJ IDEA 解析 node_modules 目录中的 webpack 需求?

javascript - 从字符串变量调用 Redux 的调度函数

javascript - 每次输入字符时输入表单都会失去焦点

javascript - 如何更改来自 React 中 props 的输入值?