首页 > 解决方案 > mapStateToProps 不更新组件

问题描述

我是一名经验丰富的 React Native 编码器,为此苦苦挣扎了 4 个小时,感觉就像在浪费生命。mapstatetoprops 不会更新组件。

代码:在组件中:

class UnreadChat extends Component {
...
  render() {
    console.log('---reloaded', this.props.unreadChatIds);//here it does not get reloaded after mapStateToProps receives 
    this.newMapStateToProps();
    return (
      <View>
        <Text>
        aa
      </Text>
      </View>
    );
  }

}

const mapStateToProps = (state) => {
    const { unreadChatIds } = state.unreadchats;
  console.log('---unreadChatIds', unreadChatIds);
    return { unreadChatIds };
};

export default connect(
  mapStateToProps, null
)(UnreadChat);

i

n 还原:

const INITIAL_STATE = {
  unreadChatIds: []
};

export default (state = JSON.parse(JSON.stringify(INITIAL_STATE)), action) => {
    switch (action.type) {
      case CHAT_SET:
        let unreadChatIds = state.unreadChatIds;
        if (action.payload.value === true) {
          if (unreadChatIds.includes(action.payload.chatId) === false) {
            unreadChatIds.push(action.payload.chatId)
          }
        }

        return { ...state, unreadChatIds };
      default:
        return state;
    }
};



Environment:
  OS: macOS High Sierra 10.13.6
  Node: 8.10.0
  Yarn: 1.5.1
  npm: 5.6.0
  Watchman: 4.9.0
  Xcode: Xcode 9.4.1 Build version 9F2000

Packages: (wanted => installed)
  react: 16.2.0 => 16.2.0
  react-native: 0.54.4 => 0.54.4 
    "react-redux": "^5.0.7",
    "redux": "^3.7.2"

你能帮我解决一下吗?mapstatetoprops 不再被使用了吗?当 redux 状态发生变化时,有什么方法可以更新组件?

谢谢

标签: react-nativeredux

解决方案


推荐阅读