React-router urls don't work when refreshing or writing manually

前端 未结 30 2716
时光取名叫无心
时光取名叫无心 2020-11-21 05:07

I\'m using React-router and it works fine while I\'m clicking on link buttons, but when I refresh my webpage it does not load what I want.

For instance, I am in

相关标签:
30条回答
  • 2020-11-21 05:39

    I am using WebPack, I had same problem Solution=> In your server.js file

    const express = require('express');
    const app = express();
    
    app.use(express.static(path.resolve(__dirname, '../dist')));
      app.get('*', function (req, res) {
        res.sendFile(path.resolve(__dirname, '../dist/index.html'));
        // res.end();
      });
    

    Why doesn't my application render after refreshing?

    0 讨论(0)
  • 2020-11-21 05:40

    For React Router V4 Users:

    If you try to solve this problem by Hash History technique mentioned in other answers, note that

    <Router history={hashHistory} >
    

    does not work in V4, please use HashRouter instead:

    import { HashRouter } from 'react-router-dom'
    
    <HashRouter>
      <App/>
    </HashRouter>
    

    Reference: HashRouter

    0 讨论(0)
  • 2020-11-21 05:41

    In your index.html head, add the following:

    <base href="/">
    <!-- This must come before the css and javascripts -->
    

    Then when running with webpack dev server use this command.

    webpack-dev-server --mode development --hot --inline --content-base=dist --history-api-fallback
    

    --history-api-fallback is the important part

    0 讨论(0)
  • 2020-11-21 05:42

    If you are hosting your react app on IIS, just add a web.config file containing :

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <httpErrors errorMode="Custom" existingResponse="Replace">
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" path="/" responseMode="ExecuteURL" />
        </httpErrors>
      </system.webServer>
    </configuration>
    

    This will tell IIS server to return the main page to the client instead of 404 error and no need to use hash history.

    0 讨论(0)
  • 2020-11-21 05:43

    The Webpack Dev Server has an option to enable this. Open up package.json and add --history-api-fallback. This solutions worked for me.

    react-router-tutorial

    0 讨论(0)
  • 2020-11-21 05:44

    The router can be called in two different ways, depending on whether the navigation occurs on the client or on the server. You have it configured for client-side operation. The key parameter is the second one to the run method, the location.

    When you use the React Router Link component, it blocks browser navigation and calls transitionTo to do a client-side navigation. You are using HistoryLocation, so it uses the HTML5 history API to complete the illusion of navigation by simulating the new URL in the address bar. If you're using older browsers, this won't work. You would need to use the HashLocation component.

    When you hit refresh, you bypass all of the React and React Router code. The server gets the request for /joblist and it must return something. On the server you need to pass the path that was requested to the run method in order for it to render the correct view. You can use the same route map, but you'll probably need a different call to Router.run. As Charles points out, you can use URL rewriting to handle this. Another option is to use a node.js server to handle all requests and pass the path value as the location argument.

    In express, for example, it might look like this:

    var app = express();
    
    app.get('*', function (req, res) { // This wildcard method handles all requests
    
        Router.run(routes, req.path, function (Handler, state) {
            var element = React.createElement(Handler);
            var html = React.renderToString(element);
            res.render('main', { content: html });
        });
    });
    

    Note that the request path is being passed to run. To do this, you'll need to have a server-side view engine that you can pass the rendered HTML to. There are a number of other considerations using renderToString and in running React on the server. Once the page is rendered on the server, when your app loads in the client, it will render again, updating the server-side rendered HTML as needed.

    0 讨论(0)
提交回复
热议问题