给定一个 App.js 文件,其中包含这些用于 redux 的代码片段:
const initialState = {
selection: ''
}
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'selection1':
return { selection: 'selection1' }
case 'selection2':
return { selection: 'selection2' }
}
return state
}
const store = createStore(reducer)
以及一个带有选择器和 redux 调度的 Redux.js 子组件:
<Picker onValueChange={(itemValue) => this.props.changeSelection(itemValue)}>
<Picker.Item label="Option1" value="selection1">
<Picker.Item label="Option2" value="selection2">
</Picker>
function mapDispatchToProps(dispatch) {
return {
changeSelection: () => dispatch({ type: itemValue })
}
}
我似乎无法弄清楚如何将选择器切换到的 itemValue 传递到调度中,然后更新 App.js 减速器中的状态。将 itemValue 传入 this.props.changeSelection() 然后将其设置为调度程序中的类型是否正确?