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

前端 未结 30 2720
时光取名叫无心
时光取名叫无心 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:46

    The answers here are all extremely helpful, what worked for me was configuring my Webpack server to expect the routes.

    devServer: {
       historyApiFallback: true,
       contentBase: './',
       hot: true
    },
    

    The historyApiFallback is what fixed this issue for me. Now routing works correctly and I can refresh the page or type in the URL directly. No need to worry about work arounds on your node server. This answer obviously only works if you're using webpack.

    EDIT: see my answer here for a more detailed reason why this is necessary: https://stackoverflow.com/a/37622953/5217568

提交回复
热议问题