• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


    Suspense for Data Fetching

    Experimental

    https://reactjs.org/docs/concurrent-mode-suspense.html

    React 16.6

    
    const ProfilePage = React.lazy(() => import('./ProfilePage'));
     // Lazy-loaded
    
    // Show a spinner while the profile is loading
    <Suspense fallback={<Spinner />}>
      <ProfilePage />
    </Suspense>
    
    

    Fetch-on-Render

    bad

    // In a function component:
    useEffect(() => {
      fetchSomething();
    }, []);
    
    // Or, in a class component:
    componentDidMount() {
      fetchSomething();
    }
    
    
    
    function ProfilePage() {
      const [user, setUser] = useState(null);
    
      useEffect(() => {
        fetchUser().then(u => setUser(u));
      }, []);
    
      if (user === null) {
        return <p>Loading profile...</p>;
      }
      return (
        <>
          <h1>{user.name}</h1>
          <ProfileTimeline />
        </>
      );
    }
    
    function ProfileTimeline() {
      const [posts, setPosts] = useState(null);
    
      useEffect(() => {
        fetchPosts().then(p => setPosts(p));
      }, []);
    
      if (posts === null) {
        return <h2>Loading posts...</h2>;
      }
      return (
        <ul>
          {posts.map(post => (
            <li key={post.id}>{post.text}</li>
          ))}
        </ul>
      );
    }
    
    

    Fetch-Then-Render

    Promise.all & react hooks

    better

    
    function fetchProfileData() {
      return Promise.all([
        fetchUser(),
        fetchPosts()
      ]).then(([user, posts]) => {
        return {user, posts};
      })
    }
    
    
    // Kick off fetching as early as possible
    const promise = fetchProfileData();
    
    function ProfilePage() {
      const [user, setUser] = useState(null);
      const [posts, setPosts] = useState(null);
    
      useEffect(() => {
        promise.then(data => {
          setUser(data.user);
          setPosts(data.posts);
        });
      }, []);
    
      if (user === null) {
        return <p>Loading profile...</p>;
      }
      return (
        <>
          <h1>{user.name}</h1>
          <ProfileTimeline posts={posts} />
        </>
      );
    }
    
    // The child doesn't trigger fetching anymore
    function ProfileTimeline({ posts }) {
      if (posts === null) {
        return <h2>Loading posts...</h2>;
      }
      return (
        <ul>
          {posts.map(post => (
            <li key={post.id}>{post.text}</li>
          ))}
        </ul>
      );
    }
    
    

    Render-as-You-Fetch / 按需渲染

    Suspense

    good

    // This is not a Promise. It's a special object from our Suspense integration.
    const resource = fetchProfileData();
    
    function ProfilePage() {
      return (
        <Suspense fallback={<h1>Loading profile...</h1>}>
          <ProfileDetails />
          <Suspense fallback={<h1>Loading posts...</h1>}>
            <ProfileTimeline />
          </Suspense>
        </Suspense>
      );
    }
    
    function ProfileDetails() {
      // Try to read user info, although it might not have loaded yet
      const user = resource.user.read();
      return <h1>{user.name}</h1>;
    }
    
    function ProfileTimeline() {
      // Try to read posts, although they might not have loaded yet
      const posts = resource.posts.read();
      return (
        <ul>
          {posts.map(post => (
            <li key={post.id}>{post.text}</li>
          ))}
        </ul>
      );
    }
    
    

    这就提出了一个问题,即在渲染下一个屏幕之前我们如何知道要获取什么?

    https://reactjs.org/blog/2019/11/06/building-great-user-experiences-with-concurrent-mode-and-suspense.html

    Errors & ErrorBoundary

    getDerivedStateFromError

    
    // Error boundaries currently have to be classes.
    class ErrorBoundary extends React.Component {
      state = { hasError: false, error: null };
      static getDerivedStateFromError(error) {
        return {
          hasError: true,
          error
        };
      }
      render() {
        if (this.state.hasError) {
          return this.props.fallback;
        }
        return this.props.children;
      }
    }
    
    
    function ProfilePage() {
      return (
        <Suspense fallback={<h1>Loading profile...</h1>}>
          <ProfileDetails />
          <ErrorBoundary fallback={<h2>Could not fetch posts.</h2>}>
            <Suspense fallback={<h1>Loading posts...</h1>}>
              <ProfileTimeline />
            </Suspense>
          </ErrorBoundary>
        </Suspense>
      );
    }
    
    

    https://aweary.dev/fault-tolerance-react/

    https://codesandbox.io/s/infallible-feather-xjtbu

    relay

    suspense integration

    https://relay.dev/docs/en/experimental/step-by-step

    https://relay.dev/docs/en/experimental/api-reference#usepreloadedquery


    fetch data with React Hooks

    https://www.robinwieruch.de/react-hooks-fetch-data

    
    
    
    
    
    
    
    
    
    
    

    Concurrent Mode

    https://reactjs.org/docs/concurrent-mode-intro.html

    react fiber

    1. 时间分片

    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    DHT(Distributed Hash Table) Translator
    Introducing shard translator
    【转】shell脚本中echo显示内容带颜色
    javac 错误: 编码GBK的不可映射字符
    一致性哈希(consistent hashing)
    在bash shell中使用getfattr查看文件扩展属性
    css3在不同型号手机浏览器上的兼容一览表
    META是什么意思?
    JS异步加载的三种方式
    AJAX中的同步加载与异步加载
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13083973.html
Copyright © 2020-2023  润新知