13

I want redirect my reactjs page to regular html page on pressing click me like link button.

Here's my code:

App.js

import React, { Component } from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import './App.css';
// import DoctorImg from './doctor.jpg';

class App extends Component {
  render() {
    return (
      <Router>
      <div>
        <Link to="/Users/yashchoksi/Documents/route/src/normal_redirect.html">Press</Link>
      <Switch>
        <Route exact path="/Users/yashchoksi/Documents/route/src/normal_redirect.html"/>
      </Switch>
    </div>
    </Router>
    );
  }
}

export default App;

Other file is

normal_redirect.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Check this.</title>
  </head>
  <body>
    <p>This is redirected page.</p>
  </body>
</html>

3 Answers 3

10

To statically link to a different page, simply use the standard HTML anchor tag:

<a href="normal_redirect.html">Redirect to Html page</a>

When the user clicks on the link, it's a normal page navigation to the href value of the tag.


To dynamically send the user to another page, use <Route> component's render property:

<Route exact path="/normal_redirect" render={() => {window.location.href="normal_redirect.html"}} />

Essentially, use the Route component's render property to tell the browser to navigate to a different page: https://reacttraining.com/react-router/web/api/Route


Try this:

class App extends Component {
  render() {
    return (
      <div>
        <a href="/Users/yashchoksi/Documents/route/src/normal_redirect.html">Press</a>
      </div>);
  }
}

Please notice how I've removed the Router, Route, Switch, and Link components from the code. For a normal link, you don't need any of them.

Sign up to request clarification or add additional context in comments.

5 Comments

Please check out the updated answer and try the second suggestion.
Do you want me to add both of the answer?
No, how do you want the user to navigate to a different url? If you want the user to click on a link and navigate, then use <a href...>. Otherwise, in case of dynamic navigation, use the <Route> component and its render property.
I want the user to click on link and navigate, and in respect of that I tried your first answer but it updates url but not content of page!
I can put screenshot if you required.
5

Your HTML must be in your public folder, in my case terminos.html

  1. In your App.js, add this code:
    <Route exact path="/terminos" render={() => {window.location.href="terminos.html"}} />
  1. If you want to add an anchor, like <a/>, add this code:
     <a target="_blank" href={process.env.PUBLIC_URL + "terminos.html"} > terminos</a>

Comments

2

As the render property is not available in React Router v5.1 (and v6), you can have a element that does the redirection:

const RedirectSite = () => {
  window.location.href = "/mypage.html";
  return <></>;
};

And then use it like this:

<Route path="/" element={<RedirectSite />} />

Reference: https://reactrouter.com/en/main/upgrading/v5

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.