Reactjs 获取用于React的Axios回调
我试图在axios get请求进行时显示加载微调器,为此,我认为需要从请求中回调。我在Reactjs 获取用于React的Axios回调,reactjs,axios,Reactjs,Axios,我试图在axios get请求进行时显示加载微调器,为此,我认为需要从请求中回调。我在componentDidMount()中调用get请求,如下所示: componentDidMount() { this.props.fetchCloudProperties( () => { this.setState( { dim : false } ); } ); } 当检索到数据时,调光器(微调器)将根据seState调用消失。我有过帖子的回调,但从来没有用get做过一次。我对数
componentDidMount()
中调用get请求,如下所示:
componentDidMount() {
this.props.fetchCloudProperties( () => {
this.setState( { dim : false } );
} );
}
当检索到数据时,调光器(微调器)将根据seState
调用消失。我有过帖子的回调,但从来没有用get做过一次。我对数据的获取操作是:
export function fetchCloudProperties( callback ) {
const VARIABLE_URL = '/cloud/properties';
const request = axios.get( `${ROOT_URL}${VARIABLE_URL}` )
.then( request => {
return ({
type : FETCH_CPS,
payload : request
});
} ).catch( ( error ) => {
console.log( error );
} )
}
现在,控制台正在抱怨“操作必须是普通对象”,我不确定这意味着什么,但我也看到它抱怨“未处理的拒绝”,所以不太确定
EDIT:我有一个createCloudProperty
方法,它使用回调,工作正常:
export function createCloudProperty( values, callback ) {
const VARIABLE_URL = '/cloud/property';
const request = axios.post( `${ROOT_URL}${VARIABLE_URL}`, values )
.then( () => callback() );
return ({
type : CREATE_CP,
payload : request
});
}
这被称为:
onSubmit( values ) {
this.props.createCloudProperty( values, () => {
this.props.history.push( '/cloudproperties' );
} );
}
您无法传递请求/响应,因为它不是普通对象。您应该使用响应数据
export function fetchCloudProperties( callback ) {
const VARIABLE_URL = '/cloud/properties';
const request = axios.get( `${ROOT_URL}${VARIABLE_URL}` )
.then( response => {
return ({
type : FETCH_CPS,
payload : response.data
});
} ).catch( ( error ) => {
console.log( error );
} )
}
您使用的是redux、flux还是其他什么?
fetchCloudProperties
是动作创建者吗?如果您正在执行异步操作,您需要使用类似于redux thunk
的东西才能从操作创建者那里返回承诺。我正在使用的@AndyRay可能与redux promise
重复,而且就我的理解,yesfetchCloudProperties()
是操作创建者。这提供了与以前相同的错误。我更新了我的问题,我是如何为一篇文章做的,效果很好。