将数组从 GraphQL 查询传递到 React-Table
Pass array from GraphQL query to React-Table
我正在尝试获取 GraphQL 查询的数组输出并使用 React-Table 呈现它,但我收到 Cannot read property 'forEach' of undefined
错误。
我设法找到的与我类似的唯一其他问题没有帮助 ()。当我将查询结果打印到控制台时,它看起来像一个普通数组,所以我不确定为什么 forEach 无法识别它。
组件代码:
import React from 'react';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';
import ReactTable from 'react-table';
import "react-table/react-table.css";
const getTemplatesQuery = gql`
{
getTemplates {
id
name
}
}
`;
const columns = [
{
Header: "ID",
accessor: "id"
},
{
Header: "Name",
accessor: "name"
}
];
const Templates = () => (
<Query
query = {getTemplatesQuery}>
{({ loading, error, data }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>error</p>;
// return data.getTemplates.map(({ id, name }) => (
// <p key={id} >{`${name}`}</p>
// ));
console.log(data);
return (
<div>
<ReactTable>
data = { data.getTemplates }
columns = { columns }
defaultPageSize = { 10 }
</ReactTable>
</div>
);
}}
</Query>
)
export default Templates;
应用代码:
...
class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<div>
<h2>My first Apollo app</h2>
<Templates />
</div>
</ApolloProvider>
);
}
}
render(<App />, document.getElementById("root"));
你的 react table 语法错误,应该是这样的
<ReactTable
data={data.getTemplates}
columns={columns}
defaultPageSize={10}
/>
我正在尝试获取 GraphQL 查询的数组输出并使用 React-Table 呈现它,但我收到 Cannot read property 'forEach' of undefined
错误。
我设法找到的与我类似的唯一其他问题没有帮助 (
组件代码:
import React from 'react';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';
import ReactTable from 'react-table';
import "react-table/react-table.css";
const getTemplatesQuery = gql`
{
getTemplates {
id
name
}
}
`;
const columns = [
{
Header: "ID",
accessor: "id"
},
{
Header: "Name",
accessor: "name"
}
];
const Templates = () => (
<Query
query = {getTemplatesQuery}>
{({ loading, error, data }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>error</p>;
// return data.getTemplates.map(({ id, name }) => (
// <p key={id} >{`${name}`}</p>
// ));
console.log(data);
return (
<div>
<ReactTable>
data = { data.getTemplates }
columns = { columns }
defaultPageSize = { 10 }
</ReactTable>
</div>
);
}}
</Query>
)
export default Templates;
应用代码:
...
class App extends Component {
render() {
return (
<ApolloProvider client={client}>
<div>
<h2>My first Apollo app</h2>
<Templates />
</div>
</ApolloProvider>
);
}
}
render(<App />, document.getElementById("root"));
你的 react table 语法错误,应该是这样的
<ReactTable
data={data.getTemplates}
columns={columns}
defaultPageSize={10}
/>