我正在研究如何在react-native中设置异步api调用。我正在使用axios。我正在ComponentDidMount
发送一个成功更改/更新我的一般状态的调度。问题是我似乎无法使用这个“数据”。
import React, { Component, PropTypes } from 'react'
import { StyleSheet, View, Text, TouchableOpacity } from 'react-native'
import { connect } from 'react-redux'
import { getMakes} from "../actions/getPrice"
import * as PriceActions from '../actions/getPrice'
@connect((store) => {
return {
cars: store.cars
};
})
export default class CounterContainer extends Component {
constructor(props) {
super(props);
this.state = {
cars: {
}
}
}
componentDidMount() {
this.props.dispatch(getMakes())
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity>
<Text style={styles.back}>Data: {this.props.cars.modelsCount}</Text>
</TouchableOpacity>
</View>
);
}
}
由于动作正常,因此我的减速机一定有问题吗?它如下:
function getPrice(state={
cars: [],
fetching: false,
fetched: false,
error: null,
}, action) {
switch (action.type) {
case "FETCH_CARS": {
return {...state, fetching: true}
}
case "FETCH_CARS_REJECTED": {
return {...state, fetching: false, error: action.payload}
}
case "FETCH_CARS_FULFILLED": {
return {
...state,
fetching: false,
fetched: true,
cars: action.payload,
}
}
}
return state
}
export default getPrice;