minternational
minternational

Reputation: 43

How to use React Router to replace a Component in render method?

I want to use React-Router to change the <Frontpage /> Component with the <Question /> Component after a click on "Los geht's".

Please ignore the handleClick method, it was one of my first tries without react-router.

import React from 'react';
import './App.css';
import Frontpage from './Frontpage'
import Question from './components/Question'
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';


class App extends React.Component {
  constructor() {
    super()
    this.state = {
      showComponent: false,
    }
    this.handeClick = this.handleClick.bind(this)
  }

  handleClick = () => {
    this.setState({
      showComponent: true })
    // console.log("The button was clicked!")
    // document.getElementById('page')
    // document.getElementsByClassName('App-Header')
  }    


  render() {
    return (
      <Router>
        <div className="App">
          <Frontpage />
            <Link to={'/Question'} className="nav-link"> Los geht's </Link>
          <Switch>
            <Route path='/Question' component={Question} />
          </Switch>
        </div>
      </Router>        
    );
  }
}


export default App;

All I get is a new Component under my "welcome page" but it needs to render the whole page in new and show the "second page".

Upvotes: 2

Views: 729

Answers (3)

Jschriemer
Jschriemer

Reputation: 625

Restructuring my answer now that desired result is a bit more clear. This should do what you are looking for.

render() {
return (
  <Router>
    <div className="App">
        <Link to="/">Frontpage</Link>
        <Link to="/Question">Question</Link>
                <Switch>
                    <Route path="/" exact component={Frontpage} />
                    <Route path="/Question" exact component={Question} />
                </Switch>
            </div>
        </Router>
      );
    }

Upvotes: 1

Jeggettx
Jeggettx

Reputation: 339

You can show <Frontpage /> only if the route exactly matches with / like so:

render() {
    return (
      <Router>
        <div className="App">
          
          <Switch>
            <Route path='/' exact >
              <Frontpage />
              <Link to={'/Question'} className="nav-link"> Los geht's </Link>
            </Route>
            <Route path='/Question' component={Question} />
          </Switch>
        </div>
      </Router>        
    );

Upvotes: 2

Clarity
Clarity

Reputation: 10873

You can achieve it like this:

render() {
    const SelectedComponent = someCondition ? Question : OtherComponent;
    return (
      <Router>
        <div className="App">
          <Frontpage />
            <Link to={'/Question'} className="nav-link"> Los geht's </Link>
          <Switch>
            <Route path='/Question' component={SelectedComponent} />
          </Switch>
        </div>
      </Router>        
    );
  }

Upvotes: 3

Related Questions