gpt4 book ai didi

reactjs - 为什么函数从不调用 redux thunk

转载 作者:行者123 更新时间:2023-12-03 14:09:42 25 4
gpt4 key购买 nike

我不明白为什么 Action 创建者被调用但卡在中间的某个地方。我的组件

import React, { Component } from 'react';
import { connect } from 'react-redux';

import {getSpots} from 'Actions';
import SpotsList from 'SpotsList'
import MapContainer from 'MapContainer';

import Alert from 'Alert';

class IndexPage extends Component{

componentWillMount() {
console.log('cwm indexpage', this.props.location.query);
getSpots('london', 1, 1)
}


render() {
return (
<div className='index-page row'>
<Alert />
<div className='col-sm-5 no-padding'>
<MapContainer />
</div>
<div className='col-sm-7 no-padding' id='right'>
<SpotsList/>
</div>
</div>
);
}
}

export default connect(null, {getSpots})(IndexPage);

以及所有 Action 创建者:

import axios from 'axios';
import {browserHistory} from 'react-router';

const ROOT_URL = '/api/spots/';

//get list of polls
export function getSpots(location='London', offset=0, sort=0){
console.log('getSpots1',location, offset, sort)
return function(dispatch){
console.log('getSpots2',location, offset, sort)
dispatch(removeErroMessage());
dispatch(changeLoadingStatus());
axios.get(ROOT_URL+'?location='+location+'&offset='+offset+'&sort='+sort+'&category_filter=bars')
.then((response)=>{
console.log('got response')
dispatch({
type: 'GET_SPOTS',
payload: response.data.businesses
});
dispatch({
type: 'SET_TERM',
payload: location
});
dispatch(setMapCenter({
lat: response.data.latitude,
lng: response.data.longitude
}));
dispatch({
type: 'SET_SPOTS_COUNT',
payload: response.data.total
});
dispatch(changeLoadingStatus());
})
.catch((error)=>{
var {status} = error.response;

if(status==400){
dispatch(setErrorMessage('Sorry! No results were found for the requested search. Try searching with some different keywords'));
}else{
dispatch(setErrorMessage('Something went wrong. We are working on it.'));
}
})
}
}

export function selectSpot(id){
return {
type: 'SELECT_SPOT',
payload: id
};
}

export function setMapCenter(coords){
return {
type: 'SET_MAP_CENTER',
payload: coords
};
}

export function setSort(sort){
return {
type: 'SET_SORT',
payload: sort
}
}


export function changeLoadingStatus(){
console.log('changeLoadingStatus')
return {
type: 'CHANGE_LOADING_STATUS'
}
}


export function setErrorMessage(error){
return {
type: 'SET_ERROR',
payload: error
}
}

export function removeErroMessage(){
console.log('removeErroMessage')
return {
type: 'REMOVE_ERROR'
}
}

输出是:

cwm indexpage Object {  }  bundle.js:29712:5
getSpots1 london 1 1

因此 getSpots 操作创建者已被调用,但没有向服务器发出请求。我的方法有什么问题吗?

最佳答案

您忘记添加调度方法来触发操作。

this.props.dispatch(getSpots('london', 1, 1));

关于reactjs - 为什么函数从不调用 redux thunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41956638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com