Search code examples
javascriptnode.jsreactjsapollo

Adding async before functional component causes it to return an object?


I have an App component and BookList component, which is rendered in App. When I add async to my BookList component I get the following error:

Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
    in BookList (at App.js:18)
    in div (at App.js:16)
    in ApolloProvider (at App.js:15)
    in App (at src/index.js:16)
    in StrictMode (at src/index.js:14) 

App:

import React from 'react';
import styled from 'styled-components';
import BookList from './components/BookList';
import ApolloClient from 'apollo-boost';
import { ApolloProvider } from '@apollo/react-hooks';

export const client = new ApolloClient({
  uri: 'http://localhost:4001/graphql',
})

const App = () => {
  return (
    <ApolloProvider client={client}>
      <div id='main'>
        <h1>Javed's Reading List</h1>
        <BookList />
      </div>
    </ApolloProvider>
  );
}

export default App;

BookList:

const BookList = async () => {

  const query = gql`
    query {
      books {
        name
        id
      }
    }
  `;

  const { data } = await client.query({ query })
  return (
    <div id='main'>
      <ul id='book-list'>
        <li>Book Name</li>
      </ul>
    </div>
  );
};

export default BookList;

I understand adding async causes the return value to be a promise, but I've done this in the past without any issues. I'm not sure what I'm missing. Why can't I render BookList?

EDIT:

I followed the advice of @MorKadosh and the useEffect hook is not firing on page load

const BookList = () => {
  const [data, setData] = useState(null)

  useEffect(() => {
    const fetch = async () => {
      const response = await client.query({ query });
      console.log(response.data);
      console.log('hello')
      setData(response.data);
    }
    fetch();
  }, [])

  return (
    <div id='main'>
      <ul id='book-list'>
        <li>Book Name</li>
      </ul>
    </div>
  );
};

export default BookList;

Solution

  • You shouldn't do that. Instead, use useEffect and useState hooks to fetch your data:

      const [data, setData] = useState(null);
    
      useEffect(() => {
        const query = ...
        const fetch = async () => {
           const response = await client.query({ query });
           setData(response.data);
        };
        fetch();
      }, []);
    

    This is just example, adjust it for your own needs.

    Why you shouldn't do that? because as you said correctly, using async will always return a Promise which is not a valid react component.