首页 > 解决方案 > 我正在从 api 查询数据,但 useffect 呈现两次,所以我无法获取信息

问题描述

我成功地从 api 获取数据,但我无法通过它进行映射,因为在应用程序上首先呈现数据数组为空,然后显示 **data.map is not a function ** 的错误

const [data, setData] = useState([]);
const [names, setNames] = useState('')

 useEffect(() => {
    fetch(
      `http://api.openweathermap.org/data/2.5/weather?q=kampala&appid=09da4ff6a7a9e25ddaa36518f64dc694`
    )
      .then((response) => response.json())
      .then((response) => setData(response));
  }, []);

  console.log(data);

data.map(dt=>setName(dt.name))

标签: reactjsapirest

解决方案


推荐阅读