gpt4 book ai didi

javascript - Gatsby React 查询 GraphQL

转载 作者:行者123 更新时间:2023-11-30 19:05:03 27 4
gpt4 key购买 nike

我正在尝试帮助正在构建网站的 friend 结合 GatsbyStrapi .我对两者都没有经验......

它的前端是用 React 制作的,它用 GraphQL 调用后端.

他有两个组成部分:<EstateSearch><FeaturedEstates> . <EstateSearch>应该从后端获取数据,然后对其进行过滤和水合物<FeaturedEstates>组件。

通常我会使用 React Context API 跨组件存储获取的数据,但我一直在阅读 here可以在构建时运行时 触发查询。但是在their example他们使用 Axios 库从外部而不是从后端获取数据。

代码

EstateSearch.js

import React, { useRef, useState } from "react";
import { graphql } from 'gatsby';
import './style.scss';

export const query = graphql`
query inmuebles {
allStrapiInmuebles(filter: {dormitorios: { eq: 2}}) {
edges {
node {
id
nombre
}
}
}
}
`;

const EstateSearch = props => {
const [ isRent, setIsRent ] = useState(true);
const [ isSale, setIsSale ] = useState(false);
const state = useRef('');
const [ cities, setCities ] = useState([]);
const city = useRef('');
const zone = useRef('');
const rooms = useRef(0);
const bathrooms = useRef(0);
const priceFrom = useRef(0);
const priceTo = useRef(0);

console.log(props.data);

return (
<div className="estateSearch">
<h3>Estate Search</h3>
<form onSubmit={submitForm}>
<button
type="button"
className={`rentButton ${!!isRent ? 'activeButton' : ''}`}
onClick={e => handleButton('rent')}
>Alquiler</button>
<button
type="button"
className={`saleButton ${!!isSale ? 'activeButton' : ''}`}
onClick={e => handleButton('sale')}
>Venta</button>

<select className="city" ref={zone}>
...
</select>

<select className="city">
<option value="" >Lorem</option>
<option value="">Ipsum</option>
</select>

<select name="city" className="city" ref={city}>
<option value="" >City</option>
<option value="">Lorem</option>
</select>

<div className="rooms">
<input type="number" name="rooms" min="0" placeholder="Rooms" ref={rooms} />
</div>
<div className="bathroom">
<input type="number" name="bathroom" min="0" placeholder="Bathrooms" ref={bathrooms} />
</div>
<div className="priceFrom">
<input type="number" name="priceFrom" min="0" placeholder="Price from" ref={priceFrom} />
</div>
<div className="priceTo">
<input type="number" name="priceTo" min="0" placeholder="Price to" ref={priceTo} />
</div>
<div className="search">
<input type="submit" value="Search" className="search" />
</div>
</form>
</div>
);

function handleButton(type) {
if (type === 'rent') {
setIsRent(true);
setIsSale(false);
} else {
setIsRent(false);
setIsSale(true);
}
}

function submitForm(e) {



e.preventDefault();
}
}

export default EstateSearch;

FeaturedEstates.js

import React from 'react'
import { Link, graphql } from 'gatsby'
import Layout from '../components/layout'
import Cover from '../components/Cover'
import FeaturedEstates from '../components/FeaturedEstates/featuredEstates'
import ServicesItems from '../components/ServicesItems/services'
import EstateCard from '../components/EstateCard/estateCard'

const IndexPage = ({ data }) => (
<Layout>

<Cover />

<FeaturedEstates>
{data.allStrapiInmuebles.edges.map(document => {
const inmueble = document.node;
let precio;
let operacion;

switch (inmueble.operacion) {
case 'Alquiler':
precio = `$${inmueble.precioAlquiler}`;
operacion = inmueble.operacion;
break;
case 'Venta':
precio = `U$S${inmueble.precioVenta}`;
operacion = inmueble.operacion;
break;
default:
precio = `$${inmueble.precioAlquiler} / U$S${inmueble.precioVenta}`;
operacion = 'Alquiler / Venta';
}

return (
<li key={document.node.id}>
<EstateCard
key={inmueble.id}
imagen={inmueble.imagendestacada.publicURL}
rooms={inmueble.dormitorios}
bathrooms={inmueble.banios}
garage={inmueble.estacionamiento}
area={inmueble.superficieConstruida}
titleBar={inmueble.nombre}
estateTipe={inmueble.tipo}
transactionTipe={operacion}
refCode="0178"
priceCard={precio}
Button= {<Link to={`/${inmueble.id}`}>Ver Mas</Link>}
/>
</li>
);
})}
</FeaturedEstates>

<ServicesItems />

</Layout>
)

export default IndexPage;

export const query = graphql`
query IndexQuery {
allStrapiInmuebles {
edges {
node {
id
nombre
dormitorios
banios
superficieConstruida
operacion
tipo
estacionamiento
precioVenta
precioAlquiler
imagendestacada {
publicURL
}
}
}
}
}

`

我想无论如何我都必须使用 Context API,但是当某些用户搜索某些内容时,我应该如何从同一个 Gatsby 服务器获取数据?

最佳答案

您将无法在生产环境中访问 Gatsby 的商店。作为静态站点生成器,它生成静态文件:)

如果您真的需要在运行时进行实时数据交互,您将需要像常规 React 应用程序一样查询实时服务器。

关于javascript - Gatsby React 查询 GraphQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59073055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com