我不明白为什么我在Redux中的减速器不希望工作。 例如,函数setSelectedItem可以工作,但setCity不需要。
控制台仅记录-ggg Bydgoszcz-或-gggToruń。
mapActions.js
export const setSelectedItem = (item) => {
return (dispatch, getState) => {
if(!item) {
dispatch({
type: actions.SET_SELECTED_ITEM_FAILED
})
} else {
dispatch({
type: actions.SET_SELECTED_ITEM_SUCCESS,
item
})
}
}
}
export const setCity = (new_city) => {
console.log("ggg", new_city)
return (dispatch, getState) => {
dispatch({
type: actions.SET_CITY,
city: new_city
})
}
}
mapReducer.js
case actions.SET_CITY:
console.log('SET_CITY', state)
return({
...state,
city: action.city
})
case actions.SET_SELECTED_ITEM_SUCCESS:
console.log('SET_SELECTED_ITEM_SUCCESS', state)
return({
...state,
selectedObject: action.item
})
case actions.SET_SELECTED_ITEM_FAILED:
console.log('SET_SELECTED_ITEM_FAILED', state)
return({
...state
})
在我叫它的地方:
<div className={styles.dropMenuCitesContent}>
<ol>
<li onClick={() => setCity("Toruń")}>Toruń</li>
<li onClick={() => setCity("Bydgoszcz")}>Bydgoszcz</li>
</ol>
</div>
[...]
const mapDispatchToProps = dispatch => {
return {
setCity: value => dispatch(setCity(value))
}
}
答案 0 :(得分:1)
尝试这个
<div className={styles.dropMenuCitesContent}>
<ol>
<li onClick={() => this.props.setCity("Toruń")}>Toruń</li>
<li onClick={() => this.props.setCity("Bydgoszcz")}>Bydgoszcz</li>
</ol>
</div>
const mapDispatchToProps = dispatch => {
return {
setCity: (value) => dispatch(setCity(value))
}
}