React Native Redux 导航道具不更新

ReactNative Redux Navigation Props not Updating

我看到 props 发生了变化,但是在返回时我的 componentDidUpdate() 方法没有被调用。不知道为什么,因为我正在使用 redux 和 returning 一个新对象。

我导航的组件:

this.handleButtonPress = async () => {
      await changeChannel(uid);
      this.props.navigation.navigate(“Chat”);
    };

减速器:

case types.SET_CURRENT_CHANNEL:
      return {
        …state,
        currentChannel: action.channel
      };
    case types.SET_PRIVATE_CHANNEL:
      return {
        …state,
        isPrivateChannel: action.isPrivateChannel
      };

操作

export const setCurrentChannel = channel => ({
    type: types.SET_CURRENT_CHANNEL,
    channel
    })

export const setPrivateChannel = isPrivateChannel => ({
    type: types.SET_PRIVATE_CHANNEL,
    isPrivateChannel
    })

export const setUserPosts = userPosts => {
  return {
    type: types.SET_USER_POSTS,
    payload: {
      userPosts
    }
  };
};

export const changeChannel = userId => {
  return dispatch => {
  const channelId = getChannelId(userId);
  dispatch(setCurrentChannel(channelId));
  dispatch(setPrivateChannel(true));
};
}
const getChannelId = userId => {
  const currentUserId = firebaseService.auth().currentUser.uid;
  return userId < currentUserId
    ? `${userId}/${currentUserId}`
    : `${currentUserId}/${userId}`;
};

我要导航回的组件:

componentDidMount() {
this.props.loadMessages(this.props.currentChannel || “MAIN”, this.props.isPrivateChannel);
    this.props.loadUsers();
  } 

render() {
    console.log(“CAN SEE CORRECT PROPS ON RERENDER”, this.props)
    const data = getChatItems(this.props.messages).reverse();

    function isEmpty(obj) {
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) return false;
      }
      return true;
    }

    return isEmpty(this.props.users) ? (
      <LoadingAnimation />
    ) : (
      <MessageListComponent data={data} users={this.props.users} 

        key={this.props.currentChannel}
        currentChannel={this.props.currentChannel}
        isPrivateChannel={this.props.isPrivateChannel}

      />
    );
  }
}

const mapStateToProps = state => ({
  messages: state.chat.messages,
  error: state.chat.loadMessagesError,
  users: state.chat.users,

  currentChannel: state.chat.currentChannel,
  isPrivateChannel: state.chat.isPrivateChannel,
  // userPosts: state.channel.userPosts,
});

const mapDispatchToProps = {
  loadMessages,
  loadUsers
};

MessagesListContainer.propTypes = {
  messages: PropTypes.object,
  users: PropTypes.object,
  error: PropTypes.string,
  loadMessages: PropTypes.func.isRequired,
  loadUsers: PropTypes.func.isRequired,

  currentChannel: PropTypes.string.isRequired,
  isPrivateChannel: PropTypes.bool.isRequired
};

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

我正在尝试 return 一个新的 currentChannel 和 isPrivateChannel。我可以看到道具在重新渲染内部发生变化,但是导航回组件并没有更新 componentDidMount() 中的道具,即使我应该发回浅拷贝或变量。

您应该改用 componentDidUpdate,因为您导航返回的组件在您离开时没有卸载,componentDidMount 仅在组件首次创建时调用一次。

componentDidUpdate(prevProps) {
  const {currentChannel and isPrivateChannel} = this.props;
  if(currentChannel && currentChannel !== prevProps.currentChannel 
    || isPrivateChannel && isPrivateChannel !== prevProps.isPrivateChannel) {

    //do something

  }
}