【发布时间】:2019-06-26 08:45:54
【问题描述】:
我的操作引发了错误:
// ACTIONS
export const startGetPrices = () => (dispatch: any) => getLatest().then((res) => {
console.log('res', res);
const { data } = res; // <-- error highlighted data
const { rates } = data;
dispatch(actionGetPrices(rates));
});
在同一个文件中,我有以下接口:
export interface IPricesRes {
data: IPriceData
}
export interface IPriceData {
base: string;
date: string;
rates: any;
success: boolean;
timestamp: number;
}
在我使用该接口的组件中:
import React from 'react'
import { connect } from 'react-redux'
import { startGetPrices, IPricesRes } from '../store'
import { CurrencySelector, Header, Prices, Navigation } from '../components'
interface IProps {
fiatPrices: [];
startGetPrices(): IPricesRes; // <-- the res interface
}
class FiatWallet extends React.PureComponent<IProps> {
componentDidMount() {
console.log('FiatWallet componentDidMount...');
this.props.startGetPrices();
}
public render() {
const { fiatPrices } = this.props;
return (
<section>
<CurrencySelector />
<Header />
<Prices prices={fiatPrices} />
<Navigation />
</section>
);
}
}
const mapDispatchToProps = (dispatch: any) => ({
startGetPrices: () => dispatch(startGetPrices())
});
const mapStateToProps = (state: any) => ({
fiatPrices: state.fiatPrices,
wallets: state.fiatPrices,
defaultCurrency: state.defaultCurrency
});
export const BoardJest = FiatWallet;
export default connect(mapStateToProps, mapDispatchToProps)(FiatWallet);
它告诉我AxiosResponse<any> 类型上不存在数据,我的问题是如何正确输入?
我在哪里使用IPricesRes 和IPriceData?
我的整个 store.ts 文件:
import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'
import { getLatest } from './services/api'
export interface IinitialState {
fiatPrices: [];
wallets: [];
defaultCurrency: string;
}
export interface IPricesRes {
data: IPriceData
}
export interface IPriceData {
base: string;
date: string;
rates: any;
success: boolean;
timestamp: number;
}
const initialState = {
fiatPrices: [],
wallets: [],
defaultCurrency: ''
}
// ACTION TYPES
export const actionTypes = {
GET_PRICES: 'GET_PRICES'
}
// REDUCER
export const reducer = (state = initialState, action: any) => {
switch (action.type) {
case actionTypes.GET_PRICES: {
const { rates } = action;
return {
...state,
fiatPrices: rates
};
}
default:
return state;
}
}
// ACTIONS CREATORS
export const actionGetPrices = (data: any) => ({
type: actionTypes.GET_PRICES,
assets: data
});
// ACTIONS
export const startGetPrices = () => (dispatch) => getLatest().then((res) => {
console.log('res', res);
const { data } = res;
const { rates } = data;
dispatch(actionGetPrices(rates));
});
// @ts-ignore
export function initializeStore(initialState: IinitialState = initialState) {
return createStore(
reducer,
initialState,
composeWithDevTools(applyMiddleware(thunkMiddleware))
)
}
getLatests() 所在的服务/api:
import axios from 'axios'
const fixerAPI = 'http://data.fixer.io/api/';
const fixerKey = '25a1ad0f5f253du7131b68cd1...';
export const getLatest = async () => {
const fixer = axios.create({
baseURL: fixerAPI,
params: {
// base: 'USD',
access_key: fixerKey
}
});
try {
const prices = await fixer.get('latest');
return prices;
} catch (err) {
console.error(err);
}
}
【问题讨论】:
标签: javascript reactjs typescript redux