As we know a react component is re-rendered when it's props or state changes.
Now i'm using useQuery
from react-apollo
package like below:
import { gql, useQuery } from '@apollo/client';
const getBookQuery = gql`
{
books {
name
}
}
`;
function BookList() {
const { loading, error, data} = useQuery(getBookQuery);
if(loading) return <p>Loading....</p>
if(error) return <p>Ops! Something went wrong</p>
return (
<>
<ul>
{data.books.map(book => (
<li key={book.name}>{book.name}</li>
))}
</ul>
</>
)
}
export default BookList;
When i run the code above, we first get Loading...
in DOM which is then updated to list containing query data (once it arrives). But how does react know to re-render my component once data is received from query.
Are these data
, loading
and error
properties mapped to component props and they are updating? If so, why doesn't chrome dev tools show any props for this BookList
component?
Can someone explain how is this useQuery
custom hook working here?