【发布时间】:2021-10-24 05:59:28
【问题描述】:
我正在开发编辑/更新功能,我想做的是在我单击表中的特定行后立即将 axios.get 的响应保存到 setState。
const [dataSystem, setdataSystem] = useState([])
const [systemDetails, setsystemDetails] = ({
ID: '',
.....,
})
const getAllSystems = async() => {
......
}
const getDependentSystems = async() => { //this is being triggered by a handleselectedSysDep when a row selected
const response = await axios.get('/API' + ID) //ID is being passed by handleselectedSysDep from the selected row
console.log('LIST OF SYSTEM', response.data)
setdataSystem(response.data)
}
const [selectedSystemID, setselectedSysID] = useState('');
let selectedSysID;
const handleselectedSysDep = (ID, action) => { //will be triggered when a row selected
setsystemDetails(ID);
selectedSysID = {...selectedSystemID, 'ID': ID.ID}
getDependentSystems();
}
useEffect(() => {
getAllSystems ();
})
我想要的是当getDependentSystems() 被调用时,setdataSystem 将被立即填充以显示 API 的响应数据,因为我正在进行编辑。希望你能帮我解决这个问题。
这是console.log('LIST OF SYSTEM', response.data)的结果
谢谢
【问题讨论】:
-
您的代码不起作用吗?你有什么问题?
ID是在哪里定义的,它是如何/何时被赋值的? -
我更新了我的代码@Phil,我的问题是我不能
setState实时从 API 获得的数据。我需要在单击按钮时显示它,因为我正在执行 EDIT/UPDATE 功能。谢谢 -
如果我理解正确,您想在单击 UPDATE 按钮时调用
getDependentSystems? -
没有@Micah,请检查我提供的代码,它是 cmets
标签: reactjs axios react-hooks