Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在ReactFire中,如何绑定依赖于属性的引用?_Reactjs_Firebase - Fatal编程技术网

Reactjs 在ReactFire中,如何绑定依赖于属性的引用?

Reactjs 在ReactFire中,如何绑定依赖于属性的引用?,reactjs,firebase,Reactjs,Firebase,我使用了一个React组件,如,其中我想使用一个Firebase引用的值,该值取决于uid。由于道具可以更改,因此我需要绑定componentWillMount和componentWillReceiveProps中的引用,如: componentWillMount(){ this.bindaObject(userRoot.child(this.props.uid).child('name'),'username'); }, 组件将接收道具(下一步){ 此参数为.unbind('username'

我使用了一个React组件,如
,其中我想使用一个Firebase引用的值,该值取决于uid。由于道具可以更改,因此我需要绑定
componentWillMount
componentWillReceiveProps中的引用,如:

componentWillMount(){
this.bindaObject(userRoot.child(this.props.uid).child('name'),'username');
},
组件将接收道具(下一步){
此参数为.unbind('username');
this.bindaObject(userRoot.child(this.props.uid).child('name'),'username');
},
render(){
返回{this.state.username['.value']};
},
React文档,大概是为了避免从两个地方更新状态


有更好的方法吗?

看起来不错。您预计
uid
将发生变化并对其作出反应

您可以考虑一些改进:

添加
uid
相等性检查,以便仅在
uid
更改时重新绑定

如果需要访问其他用户属性,请创建单个包含组件,该组件绑定到整个用户数据对象并将数据作为道具传递:

componentWillMount() {
  this.bindAsObject(userRoot.child(this.props.uid), 'user');
},

componentWillReceiveProps(nextProps) {
  if (this.props.uid !== nextProps.uid) {
    this.unbind('user');
    this.bindAsObject(userRoot.child(this.props.uid), 'user');
  }
},

render() {
  return (
    <div>
      <UserName name={this.state.user.name}></UserName>
      <Gravatar email={this.state.user.email}></Gravatar>
    </div>
  );
},
componentWillMount(){
this.bindaObject(userRoot.child(this.props.uid),'user');
},
组件将接收道具(下一步){
if(this.props.uid!==nextrops.uid){
这是。解除绑定(“用户”);
this.bindaObject(userRoot.child(this.props.uid),'user');
}
},
render(){
返回(
);
},

理想情况下,您希望所有的获取/侦听代码都在一个组件中,这样较低级别的组件就不需要关心数据是如何获取的。

这看起来不错。您预计
uid
将发生变化并对其作出反应

您可以考虑一些改进:

添加
uid
相等性检查,以便仅在
uid
更改时重新绑定

如果需要访问其他用户属性,请创建单个包含组件,该组件绑定到整个用户数据对象并将数据作为道具传递:

componentWillMount() {
  this.bindAsObject(userRoot.child(this.props.uid), 'user');
},

componentWillReceiveProps(nextProps) {
  if (this.props.uid !== nextProps.uid) {
    this.unbind('user');
    this.bindAsObject(userRoot.child(this.props.uid), 'user');
  }
},

render() {
  return (
    <div>
      <UserName name={this.state.user.name}></UserName>
      <Gravatar email={this.state.user.email}></Gravatar>
    </div>
  );
},
componentWillMount(){
this.bindaObject(userRoot.child(this.props.uid),'user');
},
组件将接收道具(下一步){
if(this.props.uid!==nextrops.uid){
这是。解除绑定(“用户”);
this.bindaObject(userRoot.child(this.props.uid),'user');
}
},
render(){
返回(
);
},

理想情况下,您希望所有的获取/侦听代码都在一个组件中,这样较低级别的组件就不需要关心数据是如何获取的。

这看起来不错。您预计
uid
将发生变化并对其作出反应

您可以考虑一些改进:

添加
uid
相等性检查,以便仅在
uid
更改时重新绑定

如果需要访问其他用户属性,请创建单个包含组件,该组件绑定到整个用户数据对象并将数据作为道具传递:

componentWillMount() {
  this.bindAsObject(userRoot.child(this.props.uid), 'user');
},

componentWillReceiveProps(nextProps) {
  if (this.props.uid !== nextProps.uid) {
    this.unbind('user');
    this.bindAsObject(userRoot.child(this.props.uid), 'user');
  }
},

render() {
  return (
    <div>
      <UserName name={this.state.user.name}></UserName>
      <Gravatar email={this.state.user.email}></Gravatar>
    </div>
  );
},
componentWillMount(){
this.bindaObject(userRoot.child(this.props.uid),'user');
},
组件将接收道具(下一步){
if(this.props.uid!==nextrops.uid){
这是。解除绑定(“用户”);
this.bindaObject(userRoot.child(this.props.uid),'user');
}
},
render(){
返回(
);
},

理想情况下,您希望所有的获取/侦听代码都在一个组件中,这样较低级别的组件就不需要关心数据是如何获取的。

这看起来不错。您预计
uid
将发生变化并对其作出反应

您可以考虑一些改进:

添加
uid
相等性检查,以便仅在
uid
更改时重新绑定

如果需要访问其他用户属性,请创建单个包含组件,该组件绑定到整个用户数据对象并将数据作为道具传递:

componentWillMount() {
  this.bindAsObject(userRoot.child(this.props.uid), 'user');
},

componentWillReceiveProps(nextProps) {
  if (this.props.uid !== nextProps.uid) {
    this.unbind('user');
    this.bindAsObject(userRoot.child(this.props.uid), 'user');
  }
},

render() {
  return (
    <div>
      <UserName name={this.state.user.name}></UserName>
      <Gravatar email={this.state.user.email}></Gravatar>
    </div>
  );
},
componentWillMount(){
this.bindaObject(userRoot.child(this.props.uid),'user');
},
组件将接收道具(下一步){
if(this.props.uid!==nextrops.uid){
这是。解除绑定(“用户”);
this.bindaObject(userRoot.child(this.props.uid),'user');
}
},
render(){
返回(
);
},
理想情况下,您希望将所有获取/侦听代码放在一个组件中,这样较低级别的组件就不需要关心数据是如何获取的