Flow爲每個參數(action.location,action.weatherResult和action.error)引發3個錯誤(未找到屬性)。我發現的唯一的解決方案是不結合,只有一個動作類型與3個不同的屬性作爲可選maybes,但屬性不是可選的,所以它不能解決我的問題。Flow抱怨Reducer中的action union類型
操作
// @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',以便有人可以運行它來重現您的確切錯誤。 –