Javascript 如何在axios()中使用useContext钩子的分派函数。然后()?
在我的组件中Javascript 如何在axios()中使用useContext钩子的分派函数。然后()?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,在我的组件中 axios({ method: 'get', url: `${SERVER_URL}crawler/get-customer/${uniqueId}`, }).then(function (response) { if(response.data){ console.log("response",response); responseData = response.data; dispatch({ type: 'SE
axios({
method: 'get',
url: `${SERVER_URL}crawler/get-customer/${uniqueId}`,
}).then(function (response) {
if(response.data){
console.log("response",response);
responseData = response.data;
dispatch({ type: 'SET_IMG', payload: { url: responseData.logo_url } });
}
},[]);
响应如期而来,但调度是递归的,几乎使我的浏览器崩溃,并且数据“url”未更新如果您可以发布整个组件,将有所帮助,但无论如何我会尝试一下 我假设您的组件也将获取的数据值移出状态(redux?),然后很可能发生这种类型的循环:
- 组件渲染
- 获取数据
- 状态中的更新值(redux?)
- 组件渲染,因为值已更新
- 获取数据
- 更新状态中的值
- 等
useEffect(() => {
axios({...}).then(...);
}, []);
通过Axiosing django本地服务器,您如何以及在哪里执行您的api请求,api请求中没有问题,我已经使用useffect()hook解决了这个问题。谢谢你的时间。不要刺伤,要求澄清,然后在我们有所有细节时回答。这实际上是一条规则吗?我觉得我清楚地列出了我的假设,这仍然会带来有益的回应。我使用了useEffect,它正在工作,谢谢你,我没有使用REDUX,只需做出反应
useEffect(() => {
axios({
method: 'get',
url: `${SERVER_URL}crawler/get-customer/${uniqueId}`,
}).then(function (response) {
if(response.data){
console.log("response",response);
responseData = response.data;
props.functionName(response.data);
}
},[]);
const mapDispatchToProps = dispatch => {
return {
functionName: (data) => dispatch({
type:'SET_IMG',
payload: { url: responseData.logo_url }
})
}
}
export default connect(, mapDispatchToProps)(nameComponent);