0

I want to create a page in such a way that it will route in such a way....... that

App.js to HomePage.js to Footer.js. Now when I try to perform this task it shows error that Router has only 1 child element.

Code for App.js:

import React, { Component } from 'react';
import logo from './logo.svg';
import HomePage from './Home_page'

import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
        <HomePage />
      </div>
    );
  }
}

export default App;

Code for Home_page.js

import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import FooterPage from './FooterPage';
class HomePage extends Component {
  render(){
    return(
    <Router>

      <div>
        <Link to='./FooterPage'>Footer Page</Link>
      </div>
      <Switch>
        <Route exact path='./FooterPage' component={FooterPage} />
      </Switch>
    </Router>
    );
  }
}
export default HomePage;

Code for Footer.js

import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
class FooterPage extends Component {
  render(){
    return(
      <div>

      </div>
    );
  }
}
export default FooterPage;
Yash Choksi
  • 483
  • 2
  • 8
  • 22

1 Answers1

1

At your Home_Page.js

import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import FooterPage from './FooterPage';
class HomePage extends Component {
  render(){
    return(
    <Router>
      <div>
        <div>
          <Link to='./FooterPage'>Footer Page</Link>
        </div>
        <Switch>
          <Route exact path='./FooterPage' component={FooterPage} />
        </Switch>
      </div>
    </Router>
    );
  }
}
export default HomePage;

here you have only one child inside Route, not two. See https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/BrowserRouter.md

Daniel Artola
  • 329
  • 3
  • 10
  • I understand what you mentioned, but why should I need to wrap my 2 elements in main div element? – Yash Choksi Feb 20 '18 at 15:17
  • Because the BrowserRouter needs a single child node, "A single child element to render.". If you want more info about why, I recommend you to check this https://reactjs.org/docs/react-api.html#react.children.only – Daniel Artola Feb 20 '18 at 15:19
  • Ok, thanks for that documentation link it will be really helpful. I will look forward for that. – Yash Choksi Feb 20 '18 at 15:21
  • By the way, if you are using react 16, you can replace the div in a react.fragment to wrap all in one child. to see more about that: https://reactjs.org/docs/fragments.html – Daniel Artola Feb 20 '18 at 15:29