Reactjs 在ReactFire中,如何绑定依赖于属性的引用?
我使用了一个React组件,如Reactjs 在ReactFire中,如何绑定依赖于属性的引用?,reactjs,firebase,Reactjs,Firebase,我使用了一个React组件,如,其中我想使用一个Firebase引用的值,该值取决于uid。由于道具可以更改,因此我需要绑定componentWillMount和componentWillReceiveProps中的引用,如: componentWillMount(){ this.bindaObject(userRoot.child(this.props.uid).child('name'),'username'); }, 组件将接收道具(下一步){ 此参数为.unbind('username'
,其中我想使用一个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(){
返回(
);
},
理想情况下,您希望将所有获取/侦听代码放在一个组件中,这样较低级别的组件就不需要关心数据是如何获取的