简体   繁体   English

使用 GraphQL 变量和 Next JS 过滤数组中的数据

[英]Filter data in array using GraphQL variables and Next JS

Trying to filter data using GraphQL variables and Next JS.尝试使用 GraphQL 变量和 Next JS 过滤数据。 How can I pass my state to the GraphQL variable "name"?如何将我的 state 传递给 GraphQL 变量“名称”?

handleSelectedFilter will update the filter state based on the selected option. handleSelectedFilter 将根据所选选项更新过滤器 state。 I am expecting the value of the filter state to get passed to "name" variable.我期待过滤器 state 的值传递给“名称”变量。

Bare in mind we cannot use react hooks inside of Next js getStaticProps function, tried state management, custom hooks..etc doesn't work with Next JS.请记住,我们不能在 Next js getStaticProps function 中使用反应钩子,尝试过 state 管理,自定义 hooks..etc 不适用于 Next JS。

Any kind of help is much appreciated.非常感谢任何形式的帮助。 Thanks in advance.提前致谢。

export async function getStaticProps() {

 const { data, errors } = await client.query({
    query: GET_LISTINGS,
    variables: {
        uri: '/listing/',
        name: filter?.city
    }
  })
  return {
    props: {
        data: data || [],
    },
    revalidate: 1
  }
}

const listings = ({ data }) => {
  const [filter, setFilter] = useState({
      city: '',
      area: '',
      type: '',
  })

  const handleSelectedFilter = e => {
    setFilter({ ...filter, [e.target.name]: e.target.value })
  }
}
export default listings

GrpahQL Schema below下面的 GrpahQL 架构

export const GET_LISTINGS = gql`
query GET_LISTINGS( $name: String) {
listingCategoriesL: listingCategories(where: {name: [$name]}) {
   nodes {
     id
     name
     listings {
       edges {
          node {
           id
           title
          }
        }
      }
    }
  }
}

With Next, your queries are processed at build time so you can't dynamically query in getStaticProps after the page is built.使用 Next,您的查询在构建时处理,因此您无法在构建页面后在getStaticProps中动态查询。 But you've got options.但你有选择。

If you want to query client-side, Apollo is probably the best choice frequently.如果你想查询客户端,Apollo 可能是经常性的最佳选择。 I wrote up a whole approach on using Apollo to fetch dynamic data in a component .写了一个完整的方法来使用 Apollo 来获取组件中的动态数据

For the select, I'd strongly recommend react-select .对于 select,我强烈推荐react-select Great project, I use it all the time.很棒的项目,我一直在使用它。 You should probably have a static set of filter handles preset for your UI (because you wouldn't want a user to just open the filter and see nothing).您可能应该为您的 UI 预设一组 static 过滤器句柄(因为您不希望用户只打开过滤器而什么也看不到)。

I'm not 100% sure what UI you're going for, but this would get you most of the way there...我不是 100% 确定你要使用什么 UI,但这会让你大部分时间都在那里......

import React from 'react'
import { useQuery, gql } from '@apollo/client'
import Select from 'react-select'

// Your query
const LISTINGS_DATA = gql`
  query GET_LISTINGS( $name: String) {
    listingCategoriesL: listingCategories(where: {name: [$name]}) {
      nodes {
        id
        name
        listings {
          edges {
            node {
              id
              title
            }
          }
        }
      }
   }
}
`

// How to style react-select
const selectStyles = {
  control: (provided, state) => ({
    ...provided,
    borderRadius: 0,
    fontWeight: 700,
    padding: '10px',
    textTransform: 'uppercase',
    minWidth: '250px'
  })
}

// Component
const Listings = ({ data }) => {

  const [filter, setFilter] = useState({
      city: '',
      area: '',
      type: '',
  })

  const handleSelectedFilter = e => {
    setFilter({ ...filter, [e.target.name]: e.target.value })
  }

  const selectOptions = [
    { value: x, label: 'Filter 1' },
  ]

  // Query for nav menu from Apollo, this is where you pass in your GraphQL variables
  const { loading, error, data } = useQuery(LISTINGS_DATA, {
    variables: {
      "name": filter,
    }
  })

  return (
   <>
     <Select 
        defaultValue={ selectOptions[0] }
        onChange={ event => handleSelectedFilter(event) }
        options={ selectOptions }
        styles={ selectStyles }
     />
   </>
  )
}
export default Listings

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

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