在表单提交时添加查询参数 - React router 4 [英] Adding query parameters on form submission - React router 4

查看:24
本文介绍了在表单提交时添加查询参数 - React router 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 React router 4 提交表单的预期效果是将搜索查询附加到 URL 的末尾.

The desired effect when submitting a form using React router 4 is to append the search query to the end of the URL.

我当前的设置将在表单提交时发送 API 请求,然后在渲染方法中渲染结果.

My current setup will send an API request on form submission, and then render the results in the render method.

不使用链接或重定向,有没有办法将查询添加到 URL 中,可能是在表单提交方法中?

Without using Links or Redirects, is there a way to add the query onto the URL, maybe from within the form submission method?

推荐答案

你可以像这样动态推送查询到 url

You can dynamically push the queries to the url like

this.props.router.push({
  pathname: '/yourRoute',
  query: { someQuery: 'value' }
})

将您的组件与 witRouter 连接,以便能够使用 router prop

Connect your Component with witRouter to be able to use the router prop

import { withRouter } from 'react-router'

....


export default withRouter(App);

这篇关于在表单提交时添加查询参数 - React router 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆