Home  >  Article  >  Web Front-end  >  Summarize an example tutorial on react-router JS controlling routing jumps

Summarize an example tutorial on react-router JS controlling routing jumps

零下一度
零下一度Original
2018-05-15 11:31:422537browse

This article mainly introduces the react-router JS control routing jump example. React can directly use react-router to implement routing. Those who are interested can learn about the

Link component, which is used for normal user clicks to jump, but sometimes operations such as form jumps and clicks on the buttonjump are also required. How to connect these situations with React Router?
The following is a form.

<form onSubmit={this.handleSubmit}>
 <input type="text" placeholder="userName"/>
 <input type="text" placeholder="repo"/>
 <button type="submit">Go</button>
</form>

The first method is to use browserHistory.push

import { browserHistory } from &#39;react-router&#39;

// ...
 handleSubmit(event) {
  event.preventDefault()
  const userName = event.target.elements[0].value
  const repo = event.target.elements[1].value
  const path = `/repos/${userName}/${repo}`
  browserHistory.push(path)
 },

The second method is to use contextobject.

export default React.createClass({

 // ask for `router` from context
 contextTypes: {
  router: React.PropTypes.object
 },

 handleSubmit(event) {
  // ...
  this.context.router.push(path)
 },
})

The above is the detailed content of Summarize an example tutorial on react-router JS controlling routing jumps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn