简体   繁体   English

React-Dom的浏览器路由器不起作用

[英]Browser Router from React-Dom not working

I am attempting to serve a react app from the public folder of my rails app. 我正在尝试从Rails应用程序的公用文件夹中提供React应用程序。 I am building the js file and putting it in the public folder. 我正在构建js文件,并将其放在公共文件夹中。 When I go to the root of the app, I can see that the js and my index.html page have loaded. 当我转到应用程序的根目录时,可以看到js和index.html页面已加载。 However, when I try to go to page, like /landing, I get a 404, route not found from Rails. 但是,当我尝试转到页面时,例如/ landing,我得到了404,从Rails中找不到路由。 I can't figure out why the react router is not kicking in. This all works on dev where I am serving the react app with a second server, I only get this issue in production. 我不知道为什么反应路由器没有启动。这一切都适用于开发人员,其中我通过第二台服务器为React应用提供服务,我只在生产中遇到此问题。 Any suggestions? 有什么建议么?

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.scss';
ReactDOM.render(<App />, document.getElementById('root'));

App.js App.js

import React from 'react';
import Auth from './util/auth';
import { Redirect, BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import MyAccount from './components/my_account';
import MyListings from './components/my_listings';
import LoginPage from './components/login_page';
import LandingPage from './components/landing_page';
import RegistrationForm from './components/registration_form';
import PasswordResetForm from './components/password_reset_form';
import RequestPasswordResetForm from './components/request_password_reset_form';
import {FlashMessages} from './components/flash_messages';

import $ from 'jquery';
import popper from 'popper.js';
import './stylesheets/App.css';
window.Popper = popper;
window.jQuery = $;
window.$ = $;
global.jQuery = $;
require('bootstrap');

const App = appProps => (
  <div>
    <div id="flash-messages">
      <FlashMessages />
    </div>
    <Router>
      <div className="App">
        <Switch>
          <Route exact name="index" path="/landing" component={LandingPage} />
          <Route exact name="login" path="/login" component={LoginPage} />
          <Route exact name="register" path="/register" component={RegistrationForm} />
          <Route exact name="reset_password" path="/reset_password" component={PasswordResetForm} />
          <Route exact name="reset_password_request" path="/reset_password_request" component={RequestPasswordResetForm} />
          <PrivateRoute path="/my_account" component={MyAccount}/>
          <PrivateRoute path="/my_listings" component={MyListings}/>
        </Switch>
      </div>
    </Router>
  </div>
);

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (
    Auth.isAuthenticated() ? (
      <Component {...props}/>
    ) : (
      <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
      }}/>
    )
  )}/>
)
export default App;

A typical gotcha with React Router is that you need to return the same index.html page for all routes - be it / , /landing-page/ or /a/really/deep/route/ . 使用React Router的典型陷阱是,您需要为所有路由返回相同的index.html页面-是//landing-page//a/really/deep/route/

You typically solve that by adding a catch-all route. 通常,您可以通过添加一条包罗万象的路线来解决此问题。 I don't know rails all that well, but I think this answer might help you out. 我不太了解,但是我认为这个答案可能会对您有所帮助。

The problem is that all routes handled first by rails and he redirects you to the page where your react routers are. 问题在于,所有路由都首先由rails处理,他将您重定向到您的react路由器所在的页面。 And you have only one HTML page that contains your react.js code. 而且,只有一个HTML页面包含您的react.js代码。 When you go to /login or any other page you get err 404 because you don't have a route in rails to handle it. 当您转到/ login或任何其他页面时,会得到err 404,因为您没有在rails中处理它的路由。 You need to add rails routes for all your pages and redirect them to the same index page Or do a catch all routes to the same index page 您需要为所有页面添加Rails路由并将它们重定向到同一索引页面,或者捕获所有路由到同一索引页面

There's some documentation for configuring your server. 有一些用于配置服务器的文档。 Basically you always need to return index.html with a 200 status code. 基本上,您总是需要返回带有200状态代码的index.html。

https://github.com/ReactTraining/react-router/blob/v3/docs/guides/Histories.md#configuring-your-server https://github.com/ReactTraining/react-router/blob/v3/docs/guides/Histories.md#configuring-your-server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM