reactjs - 为什么函数作为 React child 无效?
问题描述
我正在尝试将基于类的组件转换为功能性(无状态)组件。每次我都会收到此错误:
函数作为 React 子级无效。如果您返回 Component 而不是 ...
这是我的组件:
const ProfileStories = (props) => {
const renderStory = () => {
const data = props.userStory ? props.userStory.data : [];
if (props.isClicked) {
return (
<FlatList
data={data}
renderItem={renderStoryItem}
keyExtractor={(item, index) => index.toString()}
numColumns={2}
/>
);
} return null;
};
const renderStoryItem = (item) => {
return (
<StoryItem
key={item.id}
content={item.content}
date={item.createdAt}
/>
);
};
return (
<CenterView>
{renderStory}
</CenterView>
);
};
export default ProfileStories;
解决方案
这里:
return (
<CenterView>
{renderStory}
</CenterView>
);
您正在传递 renderStory,这确实是一个函数。您可能打算将 renderStory 函数的结果作为子函数传递给 CenterView,如下所示:
return (
<CenterView>
{renderStory()}
</CenterView>
);
推荐阅读
- spring-boot - 启动 zipkin 服务器时出错:Prometheus 要求所有具有相同名称的仪表具有相同的标签键集
- recommendation-engine - 如何从初始数据集中获取效用矩阵?
- gitlab-ci - Gitlab CI:git diff 显示旧的修改
- amazon-web-services - AWS CDK - API Gateway 上的 Cognito UserPool 授权方不起作用
- regex - 如何在 Postgres 中为文本定义正则表达式
- java-8 - LinkedIn注册:java.net.SocketException:与LinkedIn连接时连接重置
- sitecore - 在 Sitecore.Lisitmanagement.xconnect 版本 5.0.0.0 中找不到 ISubscriptionService
- wordpress - 仅基于宽度应用的wordpress大图像阈值
- ios - 函数 `DateFormatter.dateFormat(fromTemplate:)` 是如何工作的?
- java - 更改 restAPI URI