Facebook graph api 如何更新组件的结果';s根据其他组件的结果进行查询';谁的订阅?

Facebook graph api 如何更新组件的结果';s根据其他组件的结果进行查询';谁的订阅?,facebook-graph-api,graphql,react-apollo,apollo-client,scaphold,Facebook Graph Api,Graphql,React Apollo,Apollo Client,Scaphold,我在同一页上有两个react组件 组件1查询用户列表 组件2订阅事件 创建新用户的方法 我无法根据组件2的订阅结果来更新组件1的结果。 组件1: class Comp1 extends React.Component { render() { return ( <div> {this.props.data.loading? <span>loading…</span>

我在同一页上有两个react组件

  • 组件1查询用户列表
  • 组件2订阅事件 创建新用户的方法
我无法根据组件2的订阅结果来更新组件1的结果。
组件1:

class Comp1 extends React.Component {

    render() {
        return (
            <div>
            {this.props.data.loading? <span>loading…</span>
        : <span> //show the list of users </span>       }
        </div>
                    );
    }
}

export default graphql(queryAllUsers)(Comp1) ;
class Comp1扩展了React.Component{
render(){
返回(
{this.props.data.loading?正在加载…
://显示用户列表}
);
}
}
导出默认图形ql(queryAllUsers)(Comp1);
组件2:

class Comp2 extends React.Component {
   constructor(props) {
     super(props);
        this.state = {
        newUserCreated: false
            }
   }
 subscribeToNewUser() {
    this.subscription = this.props.data.subscribeToMore({
        document: subscribeToNewUsersQuery,
        variables: {//        },
    });
    }
    render() {
        return (
            <div>
            {this.state.newUserCreated}
        </div>
                    );
    }
}
类Comp2扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
newUserCreated:false
}
}
下标betonewuser(){
this.subscription=this.props.data.subscriptbetomore({
文件:subscribeToNewUsersQuery,
变量:{/},
});
}
render(){
返回(
{this.state.newUserCreated}
);
}
}

我想最简单的方法是在第一个组件中也使用查询,并通过
subscribebetomore
函数向其添加订阅

可能是这样的:

。。。
下标betonewuser(){
this.subscription=this.props.queryAllUsers.subscriptbetomore({
文件:subscribeToNewUsersQuery,
变量:{/*您的变量*/},
updateQuery:(上一个结果,{subscriptionData})=>{
const changedItem=subscriptionData.data。;
if(changedItem!==未定义&&changedItem!==空){
返回//更新以前的结果
}
返回以前的结果;
},
});
}
...
导出默认图形ql(查询器{
名称:“queryAllUsers”
})
...

(Comp1)
我想最简单的方法是在第一个组件中使用查询,并通过
subscribebetomore
函数将订阅添加到其中

可能是这样的:

。。。
下标betonewuser(){
this.subscription=this.props.queryAllUsers.subscriptbetomore({
文件:subscribeToNewUsersQuery,
变量:{/*您的变量*/},
updateQuery:(上一个结果,{subscriptionData})=>{
const changedItem=subscriptionData.data。;
if(changedItem!==未定义&&changedItem!==空){
返回//更新以前的结果
}
返回以前的结果;
},
});
}
...
导出默认图形ql(查询器{
名称:“queryAllUsers”
})
...

(Comp1)
解决方案是使用reducer函数查询第一个组件:

class Comp1扩展了React.Component{
render(){…}
}
导出默认图形ql(查询器{
选项:(ownProps)=>{
返回({
变量:{。。。
},
减速器:(以前的结果、动作)=>{
if(action.type==“阿波罗订阅结果”&&action.operationName==“订阅通迅服务”){
//更新以前的结果
返回以前的ResultWithNewData;
}否则返回以前的结果;
})
}

})(Comp1);
解决方案是使用reducer函数查询第一个组件:

class Comp1扩展了React.Component{
render(){…}
}
导出默认图形ql(查询器{
选项:(ownProps)=>{
返回({
变量:{。。。
},
减速器:(以前的结果、动作)=>{
if(action.type==“阿波罗订阅结果”&&action.operationName==“订阅通迅服务”){
//更新以前的结果
返回以前的ResultWithNewData;
}否则返回以前的结果;
})
}

})(比较1)
是的,这可以工作,但出于某些性能原因,我只想让Comp1完成一个订阅,并根据该订阅编辑其他组件的查询。然后您可以检查查询的reducer功能。在这里,您可以捕获订阅操作类型和相关订阅,并更新storet非常感谢,这正是我所寻找的。是的,这可能会起作用,但出于某些性能原因,我只希望由Comp1完成一个订阅,并根据该订阅编辑其他组件的查询。然后您可以检查查询的缩减器功能。在这里,您可以捕获订阅操作类型和相关订阅和更新商店非常感谢,这是我一直在寻找的。