React hooks are used in functional components. It means, we no longer have componentWillMount or componentDidMount. Lets find out how to do ajax call in React hook useEffect
.
const [animals, setAnimals] = React.useState([]);
React.useEffect(() => {
const fetchAnimals = async () => {
const response = await axios.get(`https://api.github.com/animals/`);
setAnimals(response.data);
}
fetchAnimals();
}, []); // This will be triggered only once.