我正在尝试发送操作以更新我的状态,我在我的组件中使用了picker
,我没有收到任何错误,但状态没有更新。有什么想法吗?
onChange = e => {
setSelectedView(e);
};
const HomeScreen = () => {
return (
<View style={styles.container}>
<Image
source={require("../img/logoGreen.png")}
style={{ width: 150, height: 100 }}
/>
<View style={{ backgroundColor: "red" }}>
<Text>Please select a year</Text>
<Picker selectedValue={0} onValueChange={onChange}>
{studentYears.map((year, i) => {
return (
<Picker.Item
key={year.id}
label={year.title}
value={year.value}
/>
);
})}
</Picker>
</View>
</View>
);
};
const mapStateToProps = state => {
return {
selectedView: state.selectedView
};
};
const mapDispatchToProps = dispatch => {
return {
setSelectedView: e => dispatch(setSelectedView(e))
};
};
export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen);
减速机:
export function setSelectedView(state = 0, action) {
switch (action.type) {
case "SET_SELECTED_VIEW":
return action.setSelectedView;
default:
return state;
}
}
动作:
export const setSelectedView = int => ({
type: "SET_SELECTED_VIEW",
selectedView: int
});
商店:
import {
eventsFetchData,
eventsHasErrored,
eventsIsLoading,
eventsFetchDataSuccess,
setSelectedView
} from "../actions/events";
import rootReducer from "../reducers";
const initialState = {
eventsHasErrored: false,
eventsIsLoading: true,
events: [],
setSelectedView: { selectedView: 0 }
};
const reduxLogger = createLogger();
const store = createStore(
rootReducer,
initialState,
applyMiddleware(thunk, reduxLogger)
);
答案 0 :(得分:0)
我认为问题在于你的减速机
export function setSelectedView(state = 0, action) {
switch (action.type) {
case "SET_SELECTED_VIEW":
return action.selectedView;//instead of action.setSelectedView;
default:
return state;
}
}