【发布时间】:2017-12-29 19:59:32
【问题描述】:
Flow 为每个参数(action.location、action.weatherResult 和 action.error)引发 3 个错误(未找到属性)。我发现的唯一解决方案是不联合,并且只有一种操作类型,其中包含 3 个不同的属性作为可选属性,但这些属性不是可选的,所以它不能解决我的问题。
动作
// @flow
import actionTypes from './index';
export type FetchWeatherStartAction = {
type: string,
location: string
};
export type FetchWeatherSuccessAction = {
type: string,
weatherResult: ?string
};
export type FetchWeatherFailAction = {
type: string,
error: string | false
};
export type WeatherAction = FetchWeatherStartAction | FetchWeatherSuccessAction | FetchWeatherFailAction;
const fetchWeatherStart = (location: string): FetchWeatherStartAction => ({
type: actionTypes.WEATHER_FETCH_START,
location
});
const fetchWeatherSuccess = (weatherResult: ?string): FetchWeatherSuccessAction => ({
type: actionTypes.WEATHER_FETCH_SUCCESS,
weatherResult
});
const fetchWeatherFail = (error: string | false): FetchWeatherFailAction => ({
type: actionTypes.WEATHER_FETCH_FAIL,
error
});
export {
fetchWeatherStart,
fetchWeatherSuccess,
fetchWeatherFail
}
动作类型
// @flow
const actionTypes = {
WEATHER_FETCH_START: 'WEATHER_FETCH_START',
WEATHER_FETCH_SUCCESS: 'WEATHER_FETCH_SUCCESS',
WEATHER_FETCH_FAIL: 'WEATHER_FETCH_FAIL'
}
export default actionTypes;
减速器
// @flow
import actionTypes from './../actions';
import type { WeatherAction } from './../actions/weather';
/*export type WeatherActionType = {
type: string,
error?: boolean | string,
weatherResult?: string | null,
location?: string
};*/
export type WeatherStateType = {
location: string,
fetchedFromServer: boolean,
isFetching: boolean,
fetchError: boolean | string,
weatherResult: ?string
};
const defaultState: WeatherStateType = {
location: 'Barcelona',
fetchedFromServer: false,
isFetching: false,
fetchError: false,
weatherResult: null
};
const weather = (state: WeatherStateType = defaultState, action: WeatherAction): WeatherStateType => {
switch (action.type) {
case actionTypes.WEATHER_FETCH_START:
return {
...state,
isFetching: true,
fetchError: false,
location: action.location
};
case actionTypes.WEATHER_FETCH_SUCCESS:
return {
...state,
fetchedFromServer: true,
isFetching: false,
fetchError: false,
weatherResult: action.weatherResult
};
case actionTypes.WEATHER_FETCH_FAIL:
return {
...state,
fetchedFromServer: false,
isFetching: false,
fetchError: action.error
};
default:
return state;
}
};
export default weather;
【问题讨论】:
-
请包含足够的导入的
actionTypes,以便有人可以运行它来重现您的确切错误。
标签: javascript reactjs redux react-redux flowtype