简体   繁体   English

如何对 redux 连接组件使用 React 延迟加载?

[英]How to use React Lazy loading for redux connected component?

I am super excited to use React.Lazy loading, however my component is using redux and I am getting warning error saying我对使用 React.Lazy 加载感到非常兴奋,但是我的组件正在使用 redux 并且我收到警告错误说

index.js:1 Warning: Functions are not valid as a React child. index.js:1 警告:函数作为 React 子级无效。 This may happen if you return a Component instead of from render.如果您返回一个组件而不是从渲染中返回,则可能会发生这种情况。 Or maybe you meant to call this function rather than return it.或者,也许您打算调用此 function 而不是返回它。

here is code for App.js这是 App.js 的代码

import React, { Suspense } from 'react';
import queryString from 'query-string';
import { LoadingSpinner } from '../components/common';
import '../assets/styles.css';

const App = () => {
  const { target } = queryString.parse(window.location.search);
  const NewsComponent = React.lazy(() => import('../components/news/News'));
  const SchoolComponent = React.lazy(() =>
    import('../components/schools/Schools')
  );
  const CalendarComponent = React.lazy(() =>
    import('../components/calendar/Calendar')
  );

  return (
    <main className="max-w-md mx-auto font-display">
      <Suspense fallback={() => <LoadingSpinner />}>
        {target === 'news' && <NewsComponent />}
        {target === 'schools' && <SchoolComponent />}
        {target === 'calendar' && <CalendarComponent />}
      </Suspense>
    </main>
  );
};

export default App;

and here is my connected component to lazy load这是我连接的延迟加载组件

import React, { useEffect, useState, MouseEvent } from 'react';
import { connect } from 'react-redux';
import { fetchNews } from '../../actions/news';
import NewsList from './NewsList';
import NewsTips from './NewsTips';
import { NewsType, StoreStateType } from '../../types';
import queryString from 'query-string';
import { Error } from '../common';

interface NewsProps {
  news: NewsType[];
  fetchNews(): void;
  isLoading: boolean;
  hasError: boolean;
}
interface NewsState {
  displayTips: boolean;
}

const News = (props: NewsProps, state: NewsState) => {
  const { news, isLoading, hasError, fetchNews } = props;
  const [displayTips, setDisplayTips] = useState(false);

  useEffect(() => {
    if (schools) {
      fetchNews();
    }

    showTips === 'true' ? setDisplayTips(true) : setDisplayTips(false);
  }, []);

  const styledImage = {
    backgroundImage:
      'url(https://engadinew-p65.test.schools.nsw.gov.au/content/dam/doe/sws/global/global-master/images/assets/school-finder.png)',
  };

  return (
    <section className="p-4">
      <h1 className="display-1 text-blue-dark mb-4">News</h1>
      {isLoading && <div>Loading</div>}
      {hasError && <Error />}
    </section>
  );
};

const mapStateToProps = ({
  news,
  isLoading,
  hasError,
}: StoreStateType): {
  news: NewsType[];
  isLoading: boolean;
  hasError: boolean;
} => {
  return { news, isLoading, hasError };
};

export default connect(mapStateToProps, { fetchNews })(News);

Can this go to production even there is a warning?这个 go 是否可以生产,即使有警告? Any idea of how to fix the warning?知道如何解决警告吗?

change this line <Suspense fallback={() => <LoadingSpinner />}> to <Suspense fallback={<LoadingSpinner />}>将此行<Suspense fallback={() => <LoadingSpinner />}>更改为<Suspense fallback={<LoadingSpinner />}>

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

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