Graphql 将Config.skip与React Apollo查询一起使用
我在使用graphql()包装中的Config.skip属性时遇到了一些问题 其目的是,只有在用户从下拉列表中选择了一项(传递关联的currentGoalID)并且(Redux)状态已使用currentGoalID的值更新后,才使用currentGoalID参数触发查询 否则,我预计(根据阿波罗文件)会: 。。。您的子组件根本不获取数据道具,并且不会调用选项或道具方法 不过,在这种情况下,由于currentGoalID没有值,似乎忽略了我的skip属性,并且调用了该选项,因为webpack编译器/linter在第51行抛出,Graphql 将Config.skip与React Apollo查询一起使用,graphql,apollo,react-apollo,Graphql,Apollo,React Apollo,我在使用graphql()包装中的Config.skip属性时遇到了一些问题 其目的是,只有在用户从下拉列表中选择了一项(传递关联的currentGoalID)并且(Redux)状态已使用currentGoalID的值更新后,才使用currentGoalID参数触发查询 否则,我预计(根据阿波罗文件)会: 。。。您的子组件根本不获取数据道具,并且不会调用选项或道具方法 不过,在这种情况下,由于currentGoalID没有值,似乎忽略了我的skip属性,并且调用了该选项,因为webpack编译器
props未定义
我成功地将currentGoalID的值记录到console.log中,而不使用graphql()
包装纸。知道config.skip为什么不起作用吗?还希望了解如何在graphql()函数调用中正确使用此函数。我在这里排除了它,但不确定上下文,谢谢
class CurrentGoal extends Component {
constructor(props) {
super(props)
}
render (){
console.log(this.props.currentGoalID);
return( <p>Current Goal: {null}</p>
)
}
}
const mapStateToProps = (state, props) => {
return {
currentGoal: state.goals.currentGoal,
currentGoalID: state.goals.currentGoalID,
currentGoalSteps: state.goals.currentGoalSteps
}
}
const FetchGoalDocByID = gql `
query root($varID:String) {
goalDocsByID(id:$varID) {
goal
}
}`;
const CurrentGoalWithState = connect(mapStateToProps)(CurrentGoal);
const CurrentGoalWithData = graphql(FetchGoalDocByID, {
skip: (props) => !props.currentGoalID,
options: {variables: {varID: props.currentGoalID}}
})(CurrentGoalWithState);
// export default CurrentGoalWithState
export default CurrentGoalWithData
类CurrentGoal扩展组件{
建造师(道具){
超级(道具)
}
渲染(){
console.log(this.props.currentGoalID);
返回(当前目标:{null}
)
}
}
常量mapStateToProps=(状态、道具)=>{
返回{
currentGoal:state.goals.currentGoal,
currentGoalID:state.goals.currentGoalID,
currentGoalSteps:state.goals.currentGoalSteps
}
}
常量FetchGoalDocByID=gql`
查询根($varID:String){
goalDocsByID(id:$varID){
目标
}
}`;
const CurrentGoalWithState=connect(mapStateToProps)(CurrentGoal);
const CurrentGoalWithData=graphql(FetchGoalDocByID{
跳过:(props)=>!props.currentGoalID,
选项:{变量:{varID:props.currentGoalID}
})(当前目标与状态);
//导出默认CurrentGoalWithState
导出默认CurrentGoalWithData
请参见此处的答案:
connect
必须是在graphql
之后执行的最后一个装饰程序,以便graphql
包含Redux的道具。您能在skip函数中添加一个console.log来打印道具吗?当然,我会尝试。我通常对在一个对象中console.log的功能缺乏信心…谢谢你,@stubailo。我给它跳过:(props)=>console.log(props.currentGoalID)。和“编译失败”,同样,在下面的选项行中,未定义种子道具。这个条件本身不足以调用skip()?@Zach\u是我的名字吗?你打开了这个问题了吗?我能追踪一下吗?我也面临同样的问题。