简体   繁体   English

盖茨比Graphql跳过

[英]Gatsby Graphql Skip

I am trying to page through data in a react component in Gatsby from graphql via a button that increments state.count. 我正在尝试通过增加state.count的按钮从graphql中翻阅Gatsby的react组件中的数据。

When I click the button the state.count is incremented but it is not passed to the query. 当我单击按钮时,state.count会增加,但不会传递给查询。

Any ideas what I am doing wrong? 有什么想法我做错了吗?

pageUp=() => {
this.setState({count: this.state.count +2});
let skip=this.state.count
}

render() {
return (
  <StaticQuery
    query={graphql`
      query ListingPageQuery ($skip:Int){
        allMarkdownRemark(
          limit:2
          skip: $skip
          ) 
          {
          ...
        }
      }
    `
    }...

I see two issues in this code. 我在此代码中看到两个问题。 First: 第一:

pageUp=() => {
    let skip=this.state.count
}

The let statement causes the skip variable to be local to this function. let语句使skip变量在此函数中处于本地状态。 It's not obvious from your question what causes it to be passed on to the GraphQL query, but if it's a member variable or something else, this statement shadows it and you're setting a purely local variable whose state will be lost. 这不是从你的问题是什么原因导致它被传递到GraphQL查询显而易见的,但如果它是一个成员变量或别的东西,这句话阴影它,你设置一个纯粹的局部变量,其状态都将丢失。

Second: 第二:

this.setState({count: this.state.count +2});
let skip=this.state.count

State updates may be asynchronous , and the React documentation specifically advises against changing state the way you have it (there is a callback-based pattern that's more appropriate). 状态更新可能是异步的 ,并且React文档特别建议不要以自己的方式更改状态(存在更适合的基于回调的模式)。 The other consequence of this is that the state may not have actually updated when you get to the next line, so you're assigning count from the "old" state to the skip variable. 这样做的另一个结果是,到达下一行时,状态实际上可能尚未更新,因此您要将count从“旧”状态分配给skip变量。

Looking at the Gatsby documentation, there is a specific note that StaticQuery does not support GraphQL variables , though browsing it doesn't suggest another path that does. 查看Gatsby文档时,需要特别注意的是, StaticQuery不支持GraphQL变量 ,尽管浏览它并不建议使用另一条路径。 (Every single example that shows potentially paginated data only shows the first page.) (每个显示潜在分页数据的示例都只显示第一页。)

You can't do that with Gatsby. 您无法通过盖茨比做到这一点。 GraphQL only happens at build-time not on the client. GraphQL仅在构建时发生,而不在客户端上发生。 You'd want to grab all the content and then use JS to only show/skip what you want to show. 您想要获取所有内容,然后使用JS仅显示/跳过要显示的内容。

Looking at the Gatsby documentation, there is a specific note that StaticQuery does not support GraphQL variables, though browsing it doesn't suggest another path that does. 查看Gatsby文档时,需要特别注意的是,StaticQuery不支持GraphQL变量,尽管浏览它并不建议使用另一条路径。

Moreover as David Maze said the StaticQuery doesn't support such variables. 此外,正如David Maze所说,StaticQuery不支持此类变量。 This wouldn't work in normal queries either because such variables need to be passed in via context. 这在普通查询中也不起作用,因为此类变量需要通过上下文传递。 https://next.gatsbyjs.org/docs/creating-and-modifying-pages/ https://next.gatsbyjs.org/docs/creating-and-modifying-pages/

Only if you pass it via context it's available in pageContext and as a variable in your queries (you normally use that for templates). 仅当您通过上下文传递它时,它才可以在pageContext使用,并且可以在查询中用作变量(通常将其用于模板)。

The setState() is asynchronous. setState()是异步的。 The right way to capture the state is in a callback, also use the prev parameter to reference state inside a setState() : 捕获状态的正确方法是在回调中,还可以使用prev参数在setState()中引用状态:

this.setState(prev => ({
    count: prev.count + 2
}), () => {
    // here this.state.count was updated and safe to use
});

Reference: https://reactjs.org/docs/react-component.html#setstate 参考: https : //reactjs.org/docs/react-component.html#setstate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM