简体   繁体   English

React 可加载不渲染组件,仅显示加载功能

[英]React loadable not rendering component, only displaying the Loading function

I am using React Loadable to show a quick loading message in between displaying components.我正在使用 React Loadable 在显示组件之间显示快速加载消息。 However, in production, it is not displaying the component and is only showing the "Loading..." message.但是,在生产中,它不显示组件,只显示“正在加载...”消息。 For some reason, it works locally (localhost:3000), but it won't render when tested in a live environment.出于某种原因,它在本地工作(localhost:3000),但在实时环境中测试时不会呈现。

This is the code i have thus far:这是我到目前为止的代码:

routes.js路由.js

// Loading function component
function Loading() {
  return <div>Loading...</div>;
}

const Compose = Loadable({
  loader: () => import('./views/Apps/Email/Compose'),
  loading: Loading,
});

const Inbox = Loadable({
  loader: () => import('./views/Apps/Email/Inbox'),
  loading: Loading,
});

const Message = Loadable({
  loader: () => import('./views/Apps/Email/Message'),
  loading: Loading,
});
.....etc

App.js应用程序.js

function Loading() {
  return <div>Loading...</div>;
};

const DefaultLayout = Loadable({
  loader: () => import('./containers/DefaultLayout'),
  loading: Loading,
});

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <Switch>
        <Route exact path="/login" name="Login Page" component={Login} />
          <Route exact path="/register" name="Register Page" component={Register} />
          <Route exact path="/404" name="Page 404" component={Page404} />
          <Route exact path="/500" name="Page 500" component={Page500} />
          <Route path="/" name="Home" component={DefaultLayout} />
        </Switch>
      </BrowserRouter>
    );
  }
}

DefaultLayout.js DefaultLayout.js

 <main className="main">
        <AppBreadcrumb appRoutes={routes}/>
        <Container fluid>
          <Switch>
            {routes.map((route, idx) => {
                return route.component ? (<Route key={idx} path={route.path} exact={route.exact} name={route.name} render={props => (
                    <route.component {...props} />
                  )} />)
                  : (null);
              },
            )}
            <Redirect from="/" to="/dashboard"></Redirect>
          </Switch>
        </Container>

One more thing to mention*, I am displaying these components/routes after a user is successfully logged in using Google OAuth.还要提一件事*,在用户使用 Google OAuth 成功登录后,我将显示这些组件/路由。 Here are my redirects:这是我的重定向:

Login.js登录.js

if (postData) {
  PostData('api/v1/zuulusers', postData).then((result) => {
     let responseJson = result;
     localStorage.setItem("userData", JSON.stringify(responseJson));
     this.setState({redirect: true});
  });
  } else {}
}
render() {

  if (this.state.redirect || localStorage.getItem('userData')) {
    return (<Redirect to={'/'}/>)
}

It is the issue with react-loadable.这是 react-loadable 的问题。 This library is not recommended for using with React.不建议将此库与 React 一起使用。 For some reason this library reads the chunks not correctly as it works badly with WebPack v4+ and also this library is no more maintaining.由于某种原因,该库无法正确读取块,因为它与 WebPack v4+ 配合不佳,而且该库不再需要维护。 To fix that problem you should migrate to React.lazy (if you do not need server render) - https://reactjs.org/docs/code-splitting.html , or another library like @loadable.component - https://www.npmjs.com/package/@loadable/component .要解决这个问题,您应该迁移到 React.lazy(如果您不需要服务器渲染) - https://reactjs.org/docs/code-splitting.html或其他库,如 @loadable.component - https:// www.npmjs.com/package/@loadable/component The second advise (if you have not a big project): it is not a problem just not to minify your code, but of course it is up to you.第二个建议(如果你没有一个大项目):不缩小你的代码不是问题,但当然这取决于你。

I also faced smilar issue, but with React 16.6.0, you can be able to use React.lazy and get rid of react-loadable library.我也遇到了类似的问题,但是使用 React 16.6.0,您可以使用 React.lazy 并摆脱 react-loadable 库。

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

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