react 合并数组_在React中更新数组

I am coming from Angular 1.x and looking to update an unordered list with React / Redux.

In console.log, I am seeing the array being updated, but it doesn't seem to bind to the DOM. I have the following --

onKeyPress of an input, I have a function that pushes to messages array.

{messages.map(function(message, key){

return (

{message}

);

})}

Update

I have the following (but no luck yet) Some notes. I am using Firebase to listen for events, and add to an array. Wondering if its a bind issue? --

class Comments extends React.Component {

constructor(props, context) {

super(props, context);

this.state = {messages: this.props.messages};

}

componentDidMount() {

const path = '/comments/all';

// Firebase watches for new comments

firebase

.database()

.ref(path)

.on('child_added', (dataSnapshot) => {

this.state.messages.push(dataSnapshot.val());

this.setState({

messages: this.state.messages

});

//console.log(dataSnapshot.val());

});

}

render() {

const messages = this.state.messages;

return (

{messages.map(function(message, key){

{message}

})}

);

}

}

解决方案

You need messages to be set in the components state.

getInitialState() {

return {

messages: []

}

}

Then in your function, set the state:

this.setState({messages: updatedMessages})

and then map over the messages state, or a messages variable in render:

const messages = this.state.messages;

{messages.map(function(message, key){

etc...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值