2

I want to open FormStatus in a new page when I click a button in Form page. Below is my code.

App.js:

import Form from "./components/Form";
import FormStatus from "./components/FormStatus";
import { BrowserRouter as Router, Route } from "react-router-dom";

function App() {
  return (
    <Router>
      <div className="App">
        <Route path="/" component={Form}/>
        <Route exact path="/form-status" component={FormStatus}/>
      </div>
    </Router>
  );
}

export default App;

Form.js

import { Link } from "react-router-dom";

const Form = () => {
    return (
        <div>
            <h1>Form Page</h1>
            <Link to="/form-status"><button>click</button></Link>
        </div>
    );
}
export default Form;

FormStatus.js:

import React, {Component} from "react";

class FormStatus extends Component {
    render(){
        return(
            <h1>Form Status Page</h1>
        )
    }
}
export default FormStatus;

when I click on the button on Form component, my url changes to http://localhost:3000/form-status, but instead of opening a new page, my FormStatus component comes below Form component.

I want FormStatus to open new a page and only shows contents of my FormStatus component and not contents of Form component

5
  • Import Switch from react-router-dom and wrap your routes with it. This way, only the first match will be shown. See more at: reactrouter.com/web/api/Switch Commented Nov 11, 2020 at 20:09
  • @pratteekshaurya did you try <Link to="/form-status" target="_blank"><button>click</button></Link> ? Commented Nov 11, 2020 at 20:10
  • @MariFaleiros A new page opens, but I can still see the contents of both Form and FormStatus on this new page Commented Nov 11, 2020 at 20:19
  • @usafder I can still see the contents of both Form and FormStatus in the new page Commented Nov 11, 2020 at 20:20
  • @pratteekshaurya then try to put 'exact' in your first route Commented Nov 11, 2020 at 20:25

1 Answer 1

2

Well, to make this work, you need to change two things in your current code.

  1. Use the Switch from the react-router-dom and wrap your routes within it.
  2. Make your home page (/) route exact instead of the /form-status route, because the /form-status route also includes the leading slash (/). So since both of them will match the expected route it will render them together.

So your code should be something like this in the App.js:

import React from "react";
import Form from "./components/Form";
import FormStatus from "./components/FormStatus";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";

export default function App() {
  return (
    <Router>
      <Switch>
        <div className="App">
          <Route exact path="/" component={Form} />
          <Route path="/form-status" component={FormStatus} />
        </div>
      </Switch>
    </Router>
  );
}

Working Demo:

CodeSandbox

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

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.