首页 文章

使用Config.skip和React-Apollo查询

提问于
浏览
3

我在使用graphql()包装器中的 Config.skip 属性时遇到了一些麻烦 .

目的是仅在用户从下拉列表中选择项目(传递关联的currentGoalID)并且使用currentGoalID的值更新(Redux)状态之后,使用currentGoalID的参数触发查询 .

否则,我希望(根据Apollo文档):

...您的子组件根本没有获得数据道具,并且不调用选项或道具方法 .

在这种情况下,似乎我的skip属性被忽略,因为缺少currentGoalID的值,并且正在调用该选项,因为webpack编译器/ linter抛出第51行, props is not defined ...

我在没有graphql()包装器的情况下成功调试了current.alID的值 . 知道为什么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

1 回答

相关问题