我正在开发一个反应本地应用程序。我目前正在使用项目组件来显示数据的平面列表。但是编辑器为React.memo的第二个参数提供了一个错误,如下所示。
类型'boolean \ undefined‘不能分配到键入'boolean’。
类型'undefined‘不能分配到键入'boolean’。
const Item = React.memo(
({ icon, title }: any) => {
return (
<Box
flexDirection="row"
paddingHorizontal="l"
justifyContent="space-between"
alignItems="center"
style={{ marginTop: 35 }}
>
<Box flexDirection="row" alignItems="center" flex={1}>
{icon}
<Box marginLeft="l">
<Text variant="stackHeader">{title}</Text>
<Text
fontSize={15}
fontFamily="CrimsonRegular"
style={{ color: '#575757' }}
>
Last update: 03/06/2020
</Text>
</Box>
</Box>
<TouchableOpacity onPress={() => Clipboard.setString(title as string)}>
<FontAwesome5 name="copy" size={28} color="white" />
</TouchableOpacity>
</Box>
);
},
(prev, next) => { // error here
if (prev.title === next.title) {
return true;
}
}
);发布于 2020-12-05 09:25:33
(prev, next) => { // error here
if (prev.title === next.title) {
return true;
}
}类型记录期望此函数返回boolean。但它只是偶尔会发生。如果条件不满足,则不执行return语句,这将导致返回undefined的函数。即使undefined是假的,它也不是false的布尔值。
因此,要解决这个问题,需要使函数始终在所有条件分支上返回一个布尔值。
例如,可以向返回false的条件中添加一个else子句。
(prev, next) => {
if (prev.title === next.title) {
return true;
} else {
return false;
}
}应简化为以下几点:
(prev, next) => {
return prev.title === next.title
}发布于 2020-12-05 09:09:57
实际上,它期望布尔值返回,因此这可能会有所帮助。
(prev, next) => {
return prev.title === next.title;
}https://stackoverflow.com/questions/65155332
复制相似问题