Custom Hooks Class 32 - sarahduv-401-advanced-javascript/seattle-javascript-401d32 GitHub Wiki

React - Using Hooks

When using hooks, it is important to remember that they will have a getter and a setter. In the example below, people is a getter, and setPeople is the setter. The initial state is empty, so we put an empty array inside of useState.

We initially set the list of people to an empty list/array.

  const [people, setPeople] = useState([]);
  const [errorMessage, setErrorMessage] = useState('');

This is used so it does not render before getting the response / used in the finally clause below.

  const [loading, setLoading] = useState(false);

useEffect runs whatever function is given to it, and contains a list of triggers on which it should run. [] = on page load

  useEffect( () => { getPeople() }, []);