我正在使用React钩子进行一个简单的项目,我想从api http://geoplugin.net/json.gp获取的数据中更新状态。但是,当我尝试更新状态时,它不起作用。仅返回初始状态。
import React, { Fragment,useState,useEffect } from 'react';
import {container,Row,Table} from 'reactstrap';
import axios from 'axios';
function LiveVisitors() {
const [visitors,setVisitors] =useState([{
ip: '',
countryCode: '',
city: '',
state: '',
country: ''
}]);
const {ip,countryCode,city,state,country} = visitors;
useEffect(()=>{
getUserData()
},[])
//get user data
const getUserData = () =>{
const res= axios.get(`http://geoplugin.net/json.gp`).then(res =>{
// console.log(res)
const {geoplugin_request,
geoplugin_countryCode,
geoplugin_countryName,
geoplugin_city,
geoplugin_region
} =res.data;
const visitor = {
ip: geoplugin_request,
countryCode: geoplugin_countryCode,
city: geoplugin_city,
state: geoplugin_region,
country: geoplugin_countryName
};
setVisitors(visitors => [...visitors,visitor])
console.log(res.data)
console.log({visitor})
console.log(visitors)
}).catch (err =>{
console.log(err)
})
}
答案 0 :(得分:0)
通过在结尾处使用useEffect
和一个空数组,可以强制其仅运行一次
尝试
useEffect(()=>{
getUserData()
},[visitors]);
或不包含数组
useEffect(()=>{
getUserData()
});
它应该在每次React检测到更改时触发,希望对您有所帮助。
这是一个非常好的article,给出了很好的解释。
答案 1 :(得分:0)
当您删除破坏setState的axios部分时,您的代码将起作用。我为您准备了一个可行的示例,并添加了另一个visitor元素。
https://codesandbox.io/s/react-playground-forked-hqswi?file=/index.js
import React, { Fragment, useState, useEffect } from "react";
import ReactDOM from "react-dom";
function LiveVisitors() {
const [visitors, setVisitors] = useState([
{
ip: "",
countryCode: "",
city: "Warsaw",
state: "",
country: "Poland"
},
{
ip: "",
countryCode: "",
city: "Gdańsk",
state: "",
country: "Poland"
}
]);
const { ip, countryCode, city, state, country } = visitors;
useEffect(() => {
getUserData();
}, []);
const getUserData = () => {
//imitate fetch
setVisitors([
...visitors,
{
ip: "",
countryCode: "",
city: "Wrocław",
state: "",
country: "Poland"
}
]);
};
return (
<div>
{visitors.map((el) => (
<div>{el.city}</div>
))}
</div>
);
}
const wrapper = document.getElementById("container");
ReactDOM.render(<LiveVisitors />, wrapper);