Javascript React Hook useEffect接收到依赖项未知的函数。而是传递一个内联函数 从“react”导入{useffect} 从“axios”导入axios 从“./WeatherDisplay”导入WeatherDisplay 常数天气=({capital})=>{ const[weather,setWeather]=useffect([]) 常数a_key='c275595564c64c757cbb51ecfe3901de' 常量参数={ 访问密钥:`${a\u密钥}`, 查询:`${capital}` } 常量fetchWeather=(参数)=>{ axios .get('https://api.weatherstack.com/current“,{params}) 。然后(响应=>{ setWeather(响应数据) }) } useEffect(fetchWeather,[params]) 控制台日志(天气) 返回( {首都}的天气 ) } 导出默认天气
如何解决此问题?混合参数和挂钩Javascript React Hook useEffect接收到依赖项未知的函数。而是传递一个内联函数 从“react”导入{useffect} 从“axios”导入axios 从“./WeatherDisplay”导入WeatherDisplay 常数天气=({capital})=>{ const[weather,setWeather]=useffect([]) 常数a_key='c275595564c64c757cbb51ecfe3901de' 常量参数={ 访问密钥:`${a\u密钥}`, 查询:`${capital}` } 常量fetchWeather=(参数)=>{ axios .get('https://api.weatherstack.com/current“,{params}) 。然后(响应=>{ setWeather(响应数据) }) } useEffect(fetchWeather,[params]) 控制台日志(天气) 返回( {首都}的天气 ) } 导出默认天气,javascript,reactjs,Javascript,Reactjs,如何解决此问题?混合参数和挂钩 import { useEffect, useState } from "react" import axios from 'axios' import WeatherDisplay from './WeatherDisplay' const Weather = ({capital, params}) => { //weather params should be provided const [weather, setW
import { useEffect, useState } from "react"
import axios from 'axios'
import WeatherDisplay from './WeatherDisplay'
const Weather = ({capital, params}) => { //weather params should be provided
const [weather, setWeather] = useState([]); // weather will be []
const a_key = 'c275595564c64c757cbb51ecfe3901de';
const params = {
access_key: `${a_key}`,
query: `${capital}`
}
const fetchWeather = () => {
axios
.get('https://api.weatherstack.com/current', {params})
.then(response => {
setWeather(response.data)
})
}
useEffect(fetchWeather, []);// dependency array probably should be empty
return(
<div>
<h2>Weather in {capital}</h2>
<WeatherDisplay weather={weather}/>
</div>)
}
export default Weather
从“react”导入{useffect,useState}
从“axios”导入axios
从“./WeatherDisplay”导入WeatherDisplay
const Weather=({capital,params})=>{//应提供天气参数
const[weather,setWeather]=useState([]);//天气将是[]
常数a_key='c275595564c64c757cbb51ecfe3901de';
常量参数={
访问密钥:`${a\u密钥}`,
查询:`${capital}`
}
常量fetchWeather=()=>{
axios
.get('https://api.weatherstack.com/current“,{params})
。然后(响应=>{
setWeather(响应数据)
})
}
useffect(fetchWeather,[]);//依赖项数组可能应该为空
返回(
{首都}的天气
)
}
导出默认天气
const[weather,setWeather]=useffect([])-->const[weather,setWeather]=useState([])。另外,请检查此“如何提出适当的问题”,您可能会混淆useffect
和useState
。