How to do a redirect to another route with react-router?
For the simple answer, you can use Link
component from react-router
, instead of button
. There is ways to change the route in JS, but seems you don't need that here.
<span className="input-group-btn">
<Link to="/login" />Click to login</Link>
</span>
To do it programmatically in 1.0.x, you do like this, inside your clickHandler function:
this.history.pushState(null, 'login');
Taken from upgrade doc here
You should have this.history
placed on your route handler component by react-router
. If it child component beneath that mentioned in routes
definition, you may need pass that down further
ReactJS redirecting to another page on successful statement not working
Issue
You can't return JSX from an asynchronous callback and expect it to be rendered and take effect. The
Redirect
must be rendered in the return of the component.The reason using the
history
object wasn't working is because thehandleSubmit
handler is outside the router providing any routing context to navigate against.
Solution
- Use the
history
object to issue an imperative redirect. - Move the
HashRouter
to wrapApp
and provide a routing context for thehistory
object. In fact, you should have only one router wrapping your app to provide the routing context, so remove all other extraneous routers you may have in your app.
Example:
index.js
import { HashRouter as Router } from 'react-router-dom';
...
ReactDOM.render(
<React.StrictMode>
<Router>
<App/>
</Router>
</React.StrictMode>,
document.getElementById('root')
);
CustomerHomePage
handleSubmit() {
const usernameInput = document.getElementById("customerLoginUsername");
const passwordInput = document.getElementById("customerLoginPassword");
if (usernameInput.value !== "customer" || passwordInput.value !== "customer") {
// alert("Wrong username or password!");
} else {
this.props.history.replace('/customerhomepage');
}
}
Route/Redirect to another page in new tab using useNavigate
react-router
isn't supposed to help preserve state when opening things in a new tab. In fact, fundamentally, react
isn't supposed to help do this.
I'd approach this in two ways:
- If the data is simple enough, pass it as a query or path param using
windows.open(window.open("http://link?key="+ value +"&key2="+ value2 ..."); )
- Look into other state management methods like
localstorage
or something else
Related Topics
Call Ajax or Submit the Button When Checkbox Is Checked
How to Get the Timezone Name in JavaScript
Php - Return Confirm Within PHP Issue
415 (Unsupported Media Type) With Rest Post Request
React - How to Open Pdf File as a Href Target Blank
Reinitialize/Reset Dropzone After Form Is Submitted
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
Why Isnt Window.Location.Href= Not Forwarding to Page Using Safari
How to Access a Model Attribute With JavaScript Variable
Trying to Load a Script File from My Node Server - Get 404
Display Data Label (Legend) in Line-Chart Using Chartjs
How to Get a File() or Blob() from an Url in JavaScript
Display a Loading Icon While Images Loads
Write to a Text or Json File in React With Node
Pass a String With Double and Single Quotes as Parameter to JavaScript Function
React Setstate Not Updating State and Ui
Why Am I Getting an Expected an Assignment or Function Call and Instead Saw an Expression
How to Return Blank String If Object Is Undefined in Angularjs Forms