我正在尝试为保存在服务器上的数组发出客户端请求,通过传递React props传递数据。但是我收到了错误
警告:propType失败:提供给
children
的道具App
无效, 期望一个ReactElement。
您能否在我收到此错误时告诉我?
我在路线中定义了propType:
Home.propTypes = {
home: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string,
})).isRequired,
};
然后异步操作函数请求数据并等待响应。
async action() {
const resp = await fetch('/graphql', {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: '{home{title}}',
}),
credentials: 'include',
});
const { data } = await resp.json();
if (!data || !data.home) throw new Error('Failed to load the news feed.');
return <Home {...data} />;
HomeItemType定义:
import {
GraphQLObjectType as ObjectType,
GraphQLString as StringType,
GraphQLNonNull as NonNull,
} from 'graphql';
const HomeItemType = new ObjectType({
name: 'HomeItem',
fields: {
id: { type: StringType },
name: { type: StringType },
}
});
服务器上的数据查询(现在尽可能简单):
import HomeItemType from '../types/HomeItemType';
var data = require('./home.json')
let items = [];
const home = {
type: new List(HomeItemType),
resolve() {
items = JSON.parse(data);
return items;
}
}
export default home;