Component Logic - Fetch data

When you create a component in Clutch (either code or composition), you can fetch data using component logic or add your logic to the definition of your code component.

To add component logic, open the component configuration menu.

Select the Code option under Logic.

Add your Logic to make a fetch request. I make my fetch request in a useEffect, set the response to JSON format, and then set that data to a state using the useState.

  let [planet, setPlanet] = React.useState(null)

  React.useEffect(() => {
    fetch("https://swapi.dev/api/planets/1/")
      .then(response => response.json())
      .then(data => setPlanet(data))
  }, [])

  console.log(planet)

The console.log will display the planet data to the console.