gpt4 book ai didi

reactjs - React Native Redux 不更新组件

转载 作者:行者123 更新时间:2023-12-02 15:53:35 26 4
gpt4 key购买 nike

我正在尝试使用 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 函数

编辑:这是 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

最佳答案

Redux 状态已更新但连接组件未更新的最常见原因是 reducer 内的状态发生变化。一个非常相似的常见问题是没有认识到 Redux connect HoC 执行浅层比较。

这是因为 Redux 通过使用对象相等性来检查更改。 (如果 === 比较返回 true,则认为该对象没有更改。)

考虑以下不正确 reducer :

function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return state.visibilityFilter = action.filter;
default:
return state
}
}

由于上面的reducer改变了状态,所以不会触发更新。

正确的示例(取自 Redux documentation )如下所示:

function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
default:
return state
}
}

浅比较

除了上述不改变状态但始终为任何更改返回新状态的最佳实践之外,重要的是要记住 Redux connect 对 mapStateToProps 返回的所有对象使用这种浅层比较>.

考虑 mapStateToProps 函数的简化版本:

function mapStateToProps(state) { 
return {
selectedDinner: state.selectedDinner,
}
};

现在考虑如何将选定的晚餐传递给 setSelectedDinner 操作(再次简化):

setSelected = (meal) => {
var index = this.props.selectedDinner.indexOf(meal);

// when index === -1 we need to add this meal
if (index === -1) {
// there is no meal at index 0 so we add it
if (this.props.selectedDinner[0] === null) {
// NOTICE - the line below still references selected dinner in state!
var tempArr = this.props.selectedDinner;
tempArr[0] = meal;

// NOTICE - we are calling setSelectedDinner with the same object
// that comes from state!
this.props.setSelectedDinner(tempArr);
}
}
}

所以问题是,在你的reducer函数中你用它自己替换了selectedDinner对象,所以Redux看不到更新。

解决问题的最快更改是修改要读取的 reducer 函数(切片调用克隆您的数组):

export default (state, action) => {
switch (action.type) {
case "SET-SELECTED-DINNER":
return {
...state,
selectedDinner: action.selectedDinner.slice()
};
default:
return state;
}
};

还有许多其他小的更改可以使此代码更易于使用并且不易出现此类错误。两个简单的是:

  1. 将修改 selectedDinner 数组的逻辑移出组件,并将其放置在 reducer 中。

  2. 引入选择器

关于reactjs - React Native Redux 不更新组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com