React Apollo客户端查询抛出错误“向查询提供的数组类型无效的道具子元素提供给预期的功能”

时间:2020-06-27 23:11:24

标签: reactjs graphql react-apollo apollo-client

我正在做一个React JS项目。我的应用程序需要使用GraphQL API。所以我为此使用了Apollo客户端。当我将查询组件与ApolloProvider组件一起使用时,它会引发错误。以下是我的代码。

这是我的app.js

import React from "react";
import ReactDOM from "react-dom";
import { createBrowserHistory } from "history";
import { HashRouter, Route, Switch, Redirect } from "react-router-dom";
import AdminLayout from "./layouts/Admin";
import { ApolloProvider } from '@apollo/react-hooks';
import ApolloClient, { gql } from 'apollo-boost';

const client = new ApolloClient({
    uri: 'https://48p1r2roz4.sse.codesandbox.io',
});

const hist = createBrowserHistory();

ReactDOM.render(
    <ApolloProvider client={client}>
        <HashRouter history={hist}>
            <Switch>
                <Route path="/admin" render={(props) => <AdminLayout {...props} />} />
                <Redirect to="/admin/dashboard" />
            </Switch>
        </HashRouter>
    </ApolloProvider>,
    document.getElementById("app")
);

如您所见,我声明了Apollo客户端并将其分配给ApolloProvider组件的客户端道具。

我有一个名为RestaurantForm的组件,该组件正在使用GraphQL Query组件,如下所示。

import React from 'react';
import {Button, Card, CardBody, CardFooter, CardHeader, CardTitle, Col, Form, FormGroup, Input, Row} from "reactstrap";
import gql from 'graphql-tag';
import { Query } from 'react-apollo';


const EXCHANGE_RATES_QUERY = gql`
  {
    rates(currency: "USD") {
      currency
      rate
    }
  }
`;

class RestaurantForm extends React.Component {
    render() {
        return (
            <Query query={EXCHANGE_RATES_QUERY}>
                {({ data }) => {
                    <>
                        <div className="content">
                            <Row>
                                <Col md="4">
                                    <Card className="card-user">
                                        <div className="image">
                                            <img
                                                alt="..."
                                                src={"assets/paper-dashboard/img/damir-bosnjak.jpg"}
                                            />
                                        </div>
                                        <CardBody>
                                            <div className="author">
                                                <a href="#pablo" onClick={(e) => e.preventDefault()}>
                                                    <img
                                                        alt="..."
                                                        className="avatar border-gray"
                                                        src={"assets/paper-dashboard/img/mike.jpg"}
                                                    />
                                                    <h5 className="title">Chet Faker</h5>
                                                </a>
                                                <p className="description">@chetfaker</p>
                                            </div>
                                            <p className="description text-center">
                                                "I like the way you work it <br />
                                                No diggity <br />I wanna bag it up"
                                            </p>
                                        </CardBody>
                                        <CardFooter>
                                            <hr />
                                            <div className="button-container">
                                                <Row>
                                                    <Col className="ml-auto" lg="3" md="6" xs="6">
                                                        <h5>
                                                            12 <br />
                                                            <small>Files</small>
                                                        </h5>
                                                    </Col>
                                                    <Col className="ml-auto mr-auto" lg="4" md="6" xs="6">
                                                        <h5>
                                                            2GB <br />
                                                            <small>Used</small>
                                                        </h5>
                                                    </Col>
                                                    <Col className="mr-auto" lg="3">
                                                        <h5>
                                                            24,6$ <br />
                                                            <small>Spent</small>
                                                        </h5>
                                                    </Col>
                                                </Row>
                                            </div>
                                        </CardFooter>
                                    </Card>
                                </Col>
                                <Col md="8">
                                    <Card className="card-user">
                                        <CardHeader>
                                            <CardTitle tag="h5">Edit Profile</CardTitle>
                                        </CardHeader>
                                        <CardBody>
                                            <Form>
                                                <Row>
                                                    <Col className="pr-1" md="5">
                                                        <FormGroup>
                                                            <label>Company (disabled)</label>
                                                            <Input
                                                                defaultValue="Creative Code Inc."
                                                                disabled
                                                                placeholder="Company"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                    <Col className="px-1" md="3">
                                                        <FormGroup>
                                                            <label>Username</label>
                                                            <Input
                                                                defaultValue="michael23"
                                                                placeholder="Username"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                    <Col className="pl-1" md="4">
                                                        <FormGroup>
                                                            <label htmlFor="exampleInputEmail1">
                                                                Email address
                                                            </label>
                                                            <Input placeholder="Email" type="email" />
                                                        </FormGroup>
                                                    </Col>
                                                </Row>
                                                <Row>
                                                    <Col className="pr-1" md="6">
                                                        <FormGroup>
                                                            <label>First Name</label>
                                                            <Input
                                                                defaultValue="Chet"
                                                                placeholder="Company"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                    <Col className="pl-1" md="6">
                                                        <FormGroup>
                                                            <label>Last Name</label>
                                                            <Input
                                                                defaultValue="Faker"
                                                                placeholder="Last Name"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                </Row>
                                                <Row>
                                                    <Col md="12">
                                                        <FormGroup>
                                                            <label>Address</label>
                                                            <Input
                                                                defaultValue="Melbourne, Australia"
                                                                placeholder="Home Address"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                </Row>
                                                <Row>
                                                    <Col className="pr-1" md="4">
                                                        <FormGroup>
                                                            <label>City</label>
                                                            <Input
                                                                defaultValue="Melbourne"
                                                                placeholder="City"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                    <Col className="px-1" md="4">
                                                        <FormGroup>
                                                            <label>Country</label>
                                                            <Input
                                                                defaultValue="Australia"
                                                                placeholder="Country"
                                                                type="text"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                    <Col className="pl-1" md="4">
                                                        <FormGroup>
                                                            <label>Postal Code</label>
                                                            <Input placeholder="ZIP Code" type="number" />
                                                        </FormGroup>
                                                    </Col>
                                                </Row>
                                                <Row>
                                                    <Col md="12">
                                                        <FormGroup>
                                                            <label>About Me</label>
                                                            <Input
                                                                type="textarea"
                                                                defaultValue="Oh so, your weak rhyme You doubt I'll bother, reading into it"
                                                            />
                                                        </FormGroup>
                                                    </Col>
                                                </Row>
                                                <Row>
                                                    <div className="update ml-auto mr-auto">
                                                        <Button
                                                            className="btn-round"
                                                            color="primary"
                                                            type="submit"
                                                        >
                                                            Update Profile
                                                        </Button>
                                                    </div>
                                                </Row>
                                            </Form>
                                        </CardBody>
                                    </Card>
                                </Col>
                            </Row>
                        </div>
                    </>
                }}
                { null }
            </Query>
        )
    }
}

export default RestaurantForm;

运行我的应用程序并转到RestaurantForm组件页面时,出现以下错误。

app.js:66453 Warning: Failed prop type: Invalid prop `children` of type `array` supplied to `Query`, expected `function`.
    in Query (created by RestaurantForm)
    in RestaurantForm (created by Context.Consumer)
    in Route (created by Dashboard)
    in Switch (created by Dashboard)
    in div (created by Dashboard)
    in div (created by Dashboard)
    in Dashboard (created by Context.Consumer)
    in Route
    in Switch
    in ApolloProvider
    in Router (created by HashRouter)
    in HashRouter

如果我只是在RestaurantForm组件中实例化客户端并在componentDidMount钩子中对其进行查询,则无需使用Query组件和ApolloProvider组件,事情就可以了。但是我需要使用ApolloProvider和Query组件。我的代码有什么问题,我该如何解决?

2 个答案:

答案 0 :(得分:2)

上面的Query组件有两个子代:

  1. 函数:{({ data }) => {...}
  2. 空:{ null }

删除{ null },以便只传递一个孩子(该函数)来解决错误。

回调也需要返回

{({ data }) => {
    return (
      //here are your components
    )
}}

答案 1 :(得分:0)

该错误通常表示Query中的内容格式错误。

这是帖子https://github.com/apollographql/react-apollo/issues/2050

也许您有多余的逗号,或者即使没有Query,您的代码也无法正常工作。