简体   繁体   English

商店更新后 React redux 组件道具未更新

[英]React redux component props not updated after store update

React Native component's props are not updating after the redux store updates.在 redux store 更新后,React Native 组件的 props 不会更新。 What's bugging me about this is that it used to do so, then one fine day, it stopped working.困扰我的是它曾经这样做过,然后有一天,它停止了工作。

Dispatch is working correctly, and the store is correctly updating state without mutating it. Dispatch 工作正常,并且 store 正确地更新状态而不改变它。

I've tried returning a new object that has nothing to do with the old state, but still no luck.我尝试返回一个与旧状态无关的新对象,但仍然没有运气。 I've also tried using componentWillReceiveProps, but it won't get called.我也试过使用 componentWillReceiveProps,但它不会被调用。

Component成分

import React from 'react';
import { connect } from 'react-redux';
import { MonoText } from '../components/StyledText';
import { bindActionCreators } from 'redux';
import * as airportActions from '../../shared/redux/modules/airports';

class HomeScreen extends React.Component {
  render() {
    const { addAirports } = this.props;

    return (
      <View style={styles.container}>
        <ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}>
          <View>
            <FlatList
              horizontal={true}
              data={this.props.airports.airports}
              renderItem={({item}) => <Text>{item.key + " "}</Text>}
              // extraData={this.state}
            />
          </View>
          <Button
            onPress={() => this.props.switchAirports()}
            title="SWITCH"
            color="#841584"
            accessibilityLabel="Learn more about this purple button"
          />
        </ScrollView> 
      </View>
    );
  }

const mapStateToProps = (state) => {
  const { airports } = state
  return { airports }
};

const mapDispatchToProps = dispatch => (
  bindActionCreators(airportActions, dispatch)
);

export default connect(mapStateToProps, mapDispatchToProps)(HomeScreen);

Reducer减速器

const SWITCH = 'airports/switch';

export default function reducer(state = INITIAL_STATE, action) {
  switch (action.type) {
    case SWITCH:
      const newState = Object.assign({}, state);
      [newState.airports[0], newState.airports[1]] = [newState.airports[1], newState.airports[0]];
      return newState
    default:
      return state
  }
};

export function switchAirports() {
  return { type: SWITCH }
}

Combined Reducer组合减速机

import { combineReducers } from 'redux';
import airports from './airports';
import galleryState from './gallery'

export default combineReducers({
    galleryState,
  airports,
});

When I click on the Toggle button, I expect that the 2 first airports get switched.当我单击“切换”按钮时,我希望前 2 个机场被切换。 They do in the store, and they'll be switched in the store after I click Switch again.它们在商店中进行,我再次单击“切换”后它们将在商店中切换。 But those state changes never reflect on the props of the component.但是这些状态变化永远不会反映在组件的 props 上。

Again, this used to work, but it stopped working all of a sudden.同样,这曾经有效,但它突然停止工作。 Not only for this component, but for another one and another reducer as well.不仅对于这个组件,而且对于另一个和另一个减速器也是如此。

I know this is almost two years old, but...我知道这已经快两年了,但是......

In your Reducer code: Object.assign() only performs a shallow copy.在您的 Reducer 代码中: Object.assign()仅执行浅拷贝。 So your newState.airports array still points to the state.airports array.所以你的newState.airports数组仍然指向state.airports数组。

That means that [newState.airports[0], newState.airports[1]] = [newState.airports[1], newState.airports[0]];这意味着[newState.airports[0], newState.airports[1]] = [newState.airports[1], newState.airports[0]]; is mutating the state, preventing the view from updating .正在改变状态, 阻止视图更新

Assuming your state.airports array has a depth of one and your state isn't further nested, try:假设您的state.airports数组的深度为 1 并且您的状态没有进一步嵌套,请尝试:

...
  const newState = {...state, airports: [...state.airports]};
...

You can read more here .您可以在此处阅读更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM