Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to navigate via clickHandlers?

Tags:

I just started learning React and I'm stuck in the following scenario.

There is an input field and on clicking the search button it takes the input value and redirect to /search/search-query/some-action

I have setup the path, defined the route to the correct view. And, I was able to do hit this path using href links. But my actual requirement is to have a button and take user to this path via onClick handler.

Searched a lot and found multiple (vague) solutions like, react-navigate-mixin etc. But I couldnt find any documentation around its usages.

Can anyone point me in the right direction?

like image 702
Ivin Avatar asked Jan 31 '15 09:01

Ivin


People also ask

How do you navigate on button click in react?

To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.

How do you navigate with buttons?

Each button corresponds to the direction that you can move in a menu. For example, to move right in a menu, press the navigation button that is located on the right side. If you want to move down in a menu, press the navigation button that is located on the bottom.

How do I navigate to a page in react?

Approach 1: Using React Router Next, you want to update your handleClick function to use a Link from react-router-dom (may have to install this package if you don't already have it using npm i react-router-dom ). Give Link the same classNames that you would your button so it's styled as a button and not an anchor tag.

How do I navigate in Dom react router?

There are two ways to programmatically navigate with React Router - <Navigate /> and navigate() . You can get access to Navigate by importing it from the react-router-dom package and you can get access to navigate by using the custom useNavigate Hook.


2 Answers

I'm gonna make the assumption that you're not building a single page app and using something along the lines of React router. And that what you need to do is simply navigate to a url based on the input.

There are two main ways of doing depending on wether you want to:

  1. Style an <a> as your button:
var Component = React.createClass({   getInitialState: function() { return {query: ''} },   queryChange: function(evt) {     this.setState({query: evt.target.value});   },   _buildLinkHref: function() {     return '/search/'+this.state.query+'/some-action';   },   render: function() {     return (       <div className="component-wrapper">         <input type="text" value={this.state.query} />         <a href={this._buildLinkHref()} className="button">           Search         </a>       </div>     );   } }); 

This way you're keeping the query (value of the input) in the state. And whenever the input changes is automatically changes the href of the link.

  1. Use a <button> and redirect programatically:
var Component = React.createClass({   getInitialState: function() { return {query: ''} },   queryChange: function(evt) {     this.setState({query: evt.target.value});   },   handleSearch: function() {     window.location = '/search/'+this.state.query+'/some-action';   },   render: function() {     return (       <div className="component-wrapper">         <input type="text" value={this.state.query} />         <button onClick={this.handleSearch()} className="button">           Search         </button>       </div>     );   } }); 

This way you handle the redirect programatically by setting your desired location to window.location.

like image 104
Bogdan Dumitru Avatar answered Oct 14 '22 03:10

Bogdan Dumitru


Adding on from the existing answer may need to bind your function like so:

constructor(props) {     super(props);     this.handleSearch = this.handleSearch.bind(this); } 
like image 24
Sean Smith Avatar answered Oct 14 '22 05:10

Sean Smith