1

I am trying to add query parameters in my url, but having trouble. For visual sakes I put the Link object in the onSubmit, but I know that this is not allowed.

<form onSubmit={<Link to={{ pathname: '/me', query: { showAge: true } }}/>}>
 .....
</form>
4
  • whats the issue you are facing when using the same link line ?? Commented Feb 24, 2017 at 5:21
  • It says Expected onSubmit listener to be a function, instead got type object Commented Feb 24, 2017 at 5:27
  • you want to hit this link onSubmit ?? Commented Feb 24, 2017 at 5:27
  • when I submit the form, I want it to go to /me but also have the parameter showAge, so /me?showAge=true Commented Feb 24, 2017 at 5:29

2 Answers 2

2

Use browserHistory/hashHistoryWrite.push(), to got to specific route, onSubmit call. Put all the values in query that you want to pass, like this:

onSubmit={()=>hashHistory.push({
                 pathname: '/me',
                 query: {a: "a", b: "b", "c": c}
              })
};

or

onSubmit={()=>browserHistory.push({
                 pathname: '/me',
                 query: {a: "a", b: "b", "c": c}
              })
};
Sign up to request clarification or add additional context in comments.

3 Comments

Hm, usually when you add parameters to url there wouldn't typically be me/params instead it would be me?params
Yeah, I want to pass multiple parameters, but there is not query parameter in the browserHistory.push
check the updated ans, i thought you just want to pass only one query parameter, in case of multiple use the pathname and query to pass multiple values :)
0

I solved it by doing:

let clientId = 2;

this.props.history.push({
    pathname: '/client',
    search: "?" + new URLSearchParams({clientId: clientId}).toString()
})

or

this.props.history.push('/client?clientId=1')

I put the above code in a function and binded it in constructor so it has access to props and props.history in your case:

constructor(props){
    super(props)
    this.onSubmitClick = this.onSubmitClick.bind(this)
}

onSubmitClick(){
    this.props.history.push('/client?clientId=1')
}

and

<form onSubmit={this.onSubmitClick}>

</form>

You might also need to wrap your component in a withRouter decorator eg. export default withRouter(Component);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.