在反应中添加对象数组时使用concat而不是es6

时间:2018-06-25 01:16:25

标签: javascript reactjs ecmascript-6

下面的代码可以工作吗?看到某个地方不确定作者为什么这样做

this.setState({
  messages: [
    ...this.state.messages,
    {text: 'new messages'}
  ]
})

通常我会做

AutoSizeColumnsMode

哪个合适?

1 个答案:

答案 0 :(得分:1)

都可以。 // Permissions stuff watchPermissions = (uid) => ( (dispatch) => { getPermissions(uid + '/notificationToken', (snapshot) => { try { dispatch(loadNotificationToken(Object.values([snapshot.val()])[0])); } catch (error) { dispatch(loadNotificationToken('')); // I could call a modal here so this can be raised at any point of the flow } }); } ); // User Stuff export const watchUserData = () => ( (dispatch) => { currentUserListener((user) => { if (user !== null) { console.log('from action creator: ' + user.displayName); dispatch(loadUser(user)); dispatch(watchReminderData(user.uid)); //listener to pull reminder data dispatch(watchContactData(user.uid)); //listener to pull contact data dispatch(watchPermissions(user.uid)); //listener to pull notificationToken } else { console.log('from action creator: ' + user); dispatch(removeUser(user)); dispatch(logOutUser(false)); dispatch(NavigationActions.navigate({ routeName: 'Login' })); } }); } ); export const loadNotificationToken = (notificationToken) => ( { type: 'LOAD_NOTIFICATION_TOKEN', notificationToken, } ); 得到了更广泛的支持,但是如果您使用的是React,那么您很有可能会进行转译,对吗?因此,您更容易理解。我更喜欢传播语法,因为它较短。

两个都只做浅拷贝。也就是说,数组对象是一个新的引用,但是数组中的任何对象都是相同的引用(照常复制值类型)。