2

Trying to set up React Router V4 and The path is always returning 404

I have a basic set up

index.jsx 

    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './components/App';


    ReactDOM.render(
        <App/>,
      document.getElementById('app')
    );

App.jsx 

    import { BrowserRouter as Router, Route} from 'react-router-dom';
    import Content from './Content';
    import Contact from './Contact';

    render () {

        return (
          <div>
              <Router>
                <div>
                <Route path='/' component={Content} />
                <Route path='/contact' component={Contact} />
                </div>
              </Router>
            </div>
            )
        }

both components are basic react components 

Content.jsx / Contact is the same just different class name of Contact

    import "../css/content.css";

    import React from 'react';

    export default class Content extends React.Component {
        render(){
            return (
                <div id="content">
                    <div className="inner">
                        <div className="bgWrap">
                            <h2>Welcome</h2>
                            <p>Hey</p>

                        </div>
                    </div>
                </div>
            );
        }
    }

the Content component works on http://localhost:8080 but I get a 404 once I try /contact, the contact

Many thanks in advance

5
  • does your server return the the client code when browsing on the path /contact? Commented Jul 12, 2017 at 8:43
  • I do not think so, looking at the network i just get the response of <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Error</title> </head> <body> <pre>Cannot GET /contact</pre> </body> </html> Commented Jul 12, 2017 at 9:08
  • You need to arrange the server so that all "client handled" routes return the same thing. Commented Jul 12, 2017 at 9:09
  • Ok many thanks I shall look into that, do yo have any links handy that would point me in the right direction, Is this something i need to do in the webpack config? Commented Jul 12, 2017 at 9:11
  • 1
    You was correct, thanks very much for your help using this link I was able to set up webpack-dev-server correctly to fallback to index.html for 404s stackoverflow.com/questions/31945763/…, may need something else for production but i understand the issue Commented Jul 12, 2017 at 9:36

2 Answers 2

0

Try :

App.jsx 

import {Switch as RouterSwitch, Route} from 'react-router-dom';
import Content from './Content';
import Contact from './Contact';

const App=()=> (
          <RouterSwitch>        
            <Route path='/' component={Content} />
            <Route path='/contact' component={Contact} />         
          </RouterSwitch>
        );

export default App;

Hope it is helpful for you :)

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

2 Comments

Thanks for your response, My App.jsx also need state which I didn't show in the example( I was trying to provide the minimum code possible :) so App.js also extends from React.Component and has a contructor with super(props) etc, I did try using the switch though but it didn't work.
:(. Sorry for not helping you. I 'm making too a App use Route.And my App worked with this code. Then wait to another Solution ^^
0

thanks to Davin Tryon in the comments, to fix my issue I needed to configure webpack-dev-server to return all client routes to in my case index.html, I shall then use he router to handle my 404's. to configure webpack I followed this How to tell webpack dev server to serve index.html for any route

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.