Temp. js
import React, {useEffect, useState} from 'react';
import axios from 'axios'
const Temp=()=> {
const APP_ID='8232312'
const APP_KEY='6a4e7c08d71463dada3b481f85a9b16'
const [receipe, setReceipe] = useState([])
useEffect(() => {
console.log("use effect")
axios.get(`https://api.edamam.com/search?q=chicken&app_id=${APP_ID}&app_key=${APP_KEY}`)
.then(response => {
console.log('get receipe by promise', response)
console.log("use state variable receipes")
setReceipe(response.data)
console.log("after use state receipe = " , receipe)
})
.catch(e => console.log(e))
});
return (
<div>
</div>
)
}
export default Temp
Приложение. js
import React from 'react';
import './App.css';
import Temp from './Component/Temp';
const App = ()=> {
return (
<div className='App'>
<Temp></Temp>
</div>
);
}
export default App;
Консольный выход -
use effect
Temp.js:13 get receipe by promise {data: {…}, status: 200, statusText: "", headers: {…}, config: {…}, …}
Temp.js:14 use state variable receipes
Temp.js:16 after use state receipe = []
Temp.js:10 use effect
Temp.js:13 get receipe by promise {data: {…}, status: 200, statusText: "", headers: {…}, config: {…}, …}
Temp.js:14 use state variable receipes
Temp.js:16 after use state receipe = {q: "chicken", from: 0, to: 10, more: true, count: 168106, …}
Temp.js:10 use effect
Temp.js:13 get receipe by promise {data: {…}, status: 200, statusText: "", headers: {…}, config: {…}, …}
Temp.js:14 use state variable receipes
Temp.js:16 after use state receipe = {q: "chicken", from: 0, to: 10, more: true, count: 168106, …}
Temp.js:10 use effect
Temp.js:13 get receipe by promise {data: {…}, status: 200, statusText: "", headers: {…}, config: {…}, …}
Temp.js:14 use state variable receipes
Temp.js:16 after use state receipe = {q: "chicken", from: 0, to: 10, more: true, count: 168106, …}
на консольном выходе, для первый раз рецепт устанавливается пустым, а второй - с ответом на запрос HTTP. Но мне нужно setReceipe (response.data), чтобы установить receipe для response.data в первый раз, а также