将值为空列表的immutable设置为空列表

时间:2016-11-06 02:13:16

标签: react-native redux immutability

如果我将一个不可变的值设置为空列表的空列表,则认为不可变的值被视为已更改。有人可以解释为什么会这样吗?我认为它们都意味着null,因此不应被视为已更改。

当我使用React Native时出现了这个问题。当最初[]的状态再次设置为[]时,会触发所有相关事件。

import React, { PropTypes } from 'react';
import Meteor, { createContainer } from 'react-native-meteor';
import ChatListView from './ChatListView';
import {connect} from 'react-redux';
import { addRoomList, addRoom, updateLastMsg } from './ChatListState';

const ChatListViewContainer = (props) => {
  const {
    listReady, rooms, messages,
    userId, authToken,
    addRoomList, addRoom, updateLastMsg
  } = props;
  console.log('container', rooms);
  console.log('container', messages);
  return (
    <ChatListView
      listReady={listReady}
      rooms={rooms}
      messages={messages}
      userId={userId}
      authToken={authToken}
      addRoomList={addRoomList}
      addRoom={addRoom}
      updateLastMsg={updateLastMsg}
    />
  );
};

ChatListViewContainer.propTypes = {
  listReady: PropTypes.bool,
  rooms: PropTypes.array,
  messages: PropTypes.array,
};

const MeteorContainer = createContainer(() => {
  const handle0 = Meteor.subscribe('userChannels', Meteor.userId());
  const roomHandle = Meteor.subscribe('room', 'channels');
  const messagesHandle = Meteor.subscribe('messages', 'GENERAL');
  return {
    listReady: roomHandle.ready()&&messagesHandle.ready(),
 // the two lines below are responsible for the update of value 
 // originally rooms =[], messages =[], then something equivalent to 
 // rooms =[], messages =[] got executed
 // Then it considers the immutables as changed 
 // and triggers the mechanism of Redux

    rooms: Meteor.collection('room').find(),
    messages: Meteor.collection('messages').find(),
  };
}, ChatListViewContainer);

export default connect(
  state => ({
    userId: state.getIn(['auth','currentUser','userId']),
    authToken: state.getIn(['auth','authenticationToken'])
  }),
  dispatch => ({
    addRoomList(roomList) {
      dispatch(addRoomList(roomList));
    },
    addRoom(rid) {
      dispatch(addRoom(rid));
    },
    updateLastMsg(msg) {
      dispatch(updateLastMsg(msg));
    }
  })
)(MeteorContainer)

0 个答案:

没有答案