【发布时间】:2019-06-05 05:29:01
【问题描述】:
我正在尝试使用 react native 设置 redux,但它不会在商店更新时更新我的组件。
class Dinner extends React.Component {
componentDidUpdate() {
console.log('does not get called when store update');
}
setSelected = (meal) => {
var index = this.props.selectedDinner.indexOf(meal);
if (index === -1) {
console.log('Adding meal to selected: '+meal.name);
if (this.props.selectedDinner[0] === null) {
var tempArr = this.props.selectedDinner;
tempArr[0] = meal;
this.props.setSelectedDinner(tempArr);
} else if(this.props.selectedDinner[1] === null) {
var tempArr = this.props.selectedDinner;
tempArr[1] = meal;
this.props.setSelectedDinner(tempArr);
} else if(this.props.selectedDinner[2] === null) {
var tempArr = this.props.selectedDinner;
tempArr[2] = meal;
this.props.setSelectedDinner(tempArr);
}
} else {
console.log("removing meal from selected: "+meal.name)
var tempArr = this.props.selectedDinner;
tempArr[index] = null;
this.props.setSelectedDinner(tempArr);
}
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.forceUpdate()
};
render() {
return (
<View style={{flex: 1, width: 360, justifyContent: 'center', alignItems: 'center', paddingBottom: 20}}>
<View style={{width: 340, backgroundColor: 'white', justifyContent: 'flex-start', alignItems: 'center'}}>
<Text style={{height: 50, fontSize: 20, fontWeight: 'bold', flex: 1, justifyContent: 'center', alignItems: 'center'}}>Dinner</Text>
{
this.props.dinnerFeed.map((prop, key) =>
prop === null ?
<TouchableOpacity style={{width: 320, height: 120, backgroundColor: 'lightgrey', flex: 1, justifyContent: 'center', alignItems: 'center', zIndex: 1, marginBottom: 10, flexShrink: 0}} key={key}><LoadingMealTile /></TouchableOpacity>
:
(prop === 'none' ?
<TouchableOpacity style={{width: 320, height: 120, backgroundColor: 'lightgrey', flex: 1, justifyContent: 'center', alignItems: 'center', zIndex: 1, marginBottom: 10, flexShrink: 0}} key={key}><BlankMealTile /></TouchableOpacity>
:
this.props.selectedDinner === null || this.props.selectedDinner.indexOf(prop) === null ?
<TouchableOpacity onPress={this.setSelected.bind(this, prop)} style={{width: 320, height: 120, backgroundColor: 'lightgrey', flex: 1, justifyContent: 'center', alignItems: 'center', zIndex: 1, marginBottom: 10, flexShrink: 0}} key={key}><MealTile selected={-1} name={prop.name} id={prop.id} url={prop.url} key={key}/></TouchableOpacity>
:
<TouchableOpacity onPress={this.setSelected.bind(this, prop)} style={{width: 320, height: 120, backgroundColor: 'lightgrey', flex: 1, justifyContent: 'center', alignItems: 'center', zIndex: 1, marginBottom: 10, flexShrink: 0}} key={key}><MealTile selected={this.props.selectedDinner.indexOf(prop)} name={prop.name} id={prop.id} url={prop.url} key={key}/></TouchableOpacity>
)
)
}
<TouchableOpacity onPress={this.props.loadFeedMeals} style={{width: 320, height: 50, backgroundColor: 'lightgrey', flex: 1, justifyContent: 'center', alignItems: 'center', zIndex: 1, marginBottom: 10}}><Text style={{fontSize: 15, }}>Load More Meals</Text></TouchableOpacity>
</View>
</View>
);
}
}
function mapStateToProps(state) {
return {
dinnerFeed: state.dinnerFeed,
selectedDinner: state.selectedDinner,
}
};
function mapDispatchToProps(dispatch) {
return {
setDinnerMeals: (dinnerMeals) => dispatch(setDinnerMeals(dinnerMeals)),
setSelectedDinner: (selectedDinner) => dispatch(setSelectedDinner(selectedDinner)),
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Dinner);
setSelectedDinner 函数正确更改了 redux 存储,但组件没有调用其 componentDidUpdate 函数
edit:这里是reducer代码
export default (state, action) => {
console.log(action);
switch (action.type) {
case "SET-SELECTED-DINNER":
return {
...state,
selectedDinner: action.selectedDinner
};
default:
return state;
}
};
我相信这段代码不应该直接改变状态,因为我在 reactjs 项目中使用了这个 reducer 来进行 redux
【问题讨论】:
-
您能否添加处理 setSelectedDinner 操作的 reducer 代码。此类问题通常是由不正确的 reducer 引起的,它们会改变状态。
-
顺便说一句,我建议您在 mapStateToProps 中使用选择器,而不是直接访问状态属性。
-
抱歉延迟回复此问题。问题与您的减速器以及您如何传递 selectedDinner 对象有关。在下面查看我的编辑。
标签: reactjs react-native redux react-redux