Meteor和React的双向数据绑定

Two-Way Data Binding With Meteor and React

本文关键字:数据绑定 React Meteor      更新时间:2023-09-26

所以Meteor使用getMeteorData()而不是getInitialState(),那么我们如何进行绑定呢?考虑到此用户配置文件的姓氏:

...
getMeteorData() {
 return {
   u = Meteor.user()
 }
},
componentRender() {
  let instance = this;
  // there is no way you can have two-way binding here. She types in "Brown".
  return(<div><input value={instance.data.user.profile.surname }</div>)
},
render() {
 return(<div>{this.data.user ?  this.componentRender() : <p>Loading...</p>}</div>)
}
...

如何为此编写onChange函数?我只知道setState的做事方式。此外,该代码是单向绑定的,就好像"Sue"想更改她的姓氏一样,"Brown"不会被替换。

与React的双向绑定包括getInitialState功能。

更新:

我已经用我的中级帖子解决了我的答案。

Meteor默认情况下不会为您提供自动双向绑定。

您需要为您的输入添加一个更改时事件处理程序,以处理用户名中的更新。

在onChanged中,您可以拨打电话更新用户的姓氏。

Meteor.users.update ...

请注意,这根本没有效率,因为每次用户更改输入时都会调用更新。最好有一个按钮点击以触发更新事件。

请参阅:Meteor React更新文档