0

I was following along Ben Awad's video and tried to extend the idea to react SuspenseList but couldn't get the items to show in reverse order or together when I changed the revealOrder. In all the cases the endpoints are called one after another and they are loaded in forwards mode.

Demo Repo

const fetchPerson = () => {
    return fetch('https://randomuser.me/api').then(x => x.json()).then(x => x.results[0]); 
}; 

const wrapPromise = (promise) => {
    let status = 'pending'; 
    let result = '';
    let suspender = promise.then(r => {
        status = 'success'; 
        result = r;
    }, e => { 
        status = 'error'; 
        result = e; 
    }) 

    return {
        read(){
            if (status === 'pending'){
                throw suspender; 
            } else if (status === 'error'){
                throw result; 
            }
            return result; 
        }
    }
} 

export const createResource = () => {
    return {
        person1: wrapPromise(fetchPerson()),
        person2: wrapPromise(fetchPerson()),
        person3: wrapPromise(fetchPerson()),
        person4: wrapPromise(fetchPerson()),
        person5: wrapPromise(fetchPerson()),
        person6: wrapPromise(fetchPerson())
    }
}




import React, { Suspense } from 'react';
import './App.css';
import { createResource } from './PersonApi';
import { Person } from './Person'; 

const resource = createResource(); 

function App() {
  return (
      <div className="App">
        <Suspense fallback={<h1>loading...</h1>} revealOrder="together">
          <Suspense fallback={<h1>loading person 1</h1>}>
            <Person resource={resource} num="person1" />
          </Suspense>
          <Suspense fallback={<h1>loading person 2</h1>}>
            <Person resource={resource} num="person2" />
          </Suspense>
          <Suspense fallback={<h1>loading person 3</h1>}>
            <Person resource={resource} num="person3"/>
          </Suspense>
          <Suspense fallback={<h1>loading person 4</h1>}>
            <Person resource={resource} num="person4"/>
          </Suspense>
          <Suspense fallback={<h1>loading person 5</h1>}>
            <Person resource={resource} num="person5"/>
          </Suspense>
          <Suspense fallback={<h1>loading person 6</h1>}>
            <Person resource={resource} num="person6"/>
          </Suspense>
        </Suspense>
      </div>
  );
}

export default App;


import React from 'react'; 


export const Person = ({ resource, num  }) => {
    const person = resource[num].read(); 
    return (<div>Loaded {num}: - {person.name.first}</div>)
}; 

Rahil Ahmad
  • 3,056
  • 1
  • 16
  • 21
Yasin Yaqoobi
  • 1,888
  • 3
  • 27
  • 38
  • 1
    I don't see suspencelist in the code. Also, please note that it's alps experimental. – Karthik R Oct 30 '19 at 02:43
  • woops, silly mistake. switching to suspenseList fixed the issue, however the backwards order is still functioning like "together". I see that the components are called in the right order from bottom to top but they all show up at the same time. – Yasin Yaqoobi Oct 30 '19 at 20:56

0 Answers0