`Why use render() in reactjs?
N
N
n1ksON2020-05-06 19:22:11
React
n1ksON, 2020-05-06 19:22:11

Why use render() in reactjs?

Initially it was like this

function App() {
  gettingWeather = async() => {
    const apiUrl = await 
    fetch(`http://api.openweathermap.org/data/2.5/weather?q=Kiev&appid=${apiKey}&units=metric`);
    const data = await apiUrl.json();
    console.log(data);
  }
  return (
    <div className="App">
      <Info />
      <Weather />
      <Form 
        weatherMethod={this.gettingWeather}
      />
    </div>
  );
}

It gave an error that the method gettingWeather is not defined.
Changed App to a class and added render() there:
class App extends React.Component {
  gettingWeather = async() => {
    const apiUrl = await 
    fetch(`http://api.openweathermap.org/data/2.5/weather?q=Kiev&appid=${apiKey}&units=metric`);
    const data = await apiUrl.json();
    console.log(data);
  }
  render() {
  return (
    <div className="App">
      <Info />
      <Weather />
      <Form 
        weatherMethod={this.gettingWeather}
      />
    </div>
  );
  }
}

In the end, it worked. Can you explain in what cases it is necessary to use function and in what cases class extends with render?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Suntsev, 2020-05-06
@n1ksON

in the first case, you have an error in the line You are accessing the current function without linking it to the asynchronous method gettingWeather And about render here is the dock
weatherMethod={this.gettingWeather}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question