是否可以在内置组件标签中使用三元运算符?例如,我正在使用React Native(本机库)中的Touchable Opacity:
type ItemProps = {
title: string;
face: string;
};
export const Item: React.FunctionComponent<ItemProps> = ({
title,
face,
}) => {
const [showAddFriendPage, setShowAddFriendPage] = useState(false);
const toggleAddFriendPage = () => {
setShowAddFriendPage(showAddFriendPage ? false : true);
};
return (
<TouchableOpacity activeOpacity={0.8}
onPress={() =>
setShowAddFriendPage(true)
} >
<View>
<Thumbnail small source={{ uri: face }} style={styles.thumbnail} />
<Text numberOfLines={1} style={styles.title}>
{title}
</Text>
<AddFriendPage
showAddFriendPage={showAddFriendPage}
toggleShowPage={toggleAddFriendPage}
/>
</View>
</TouchableOpacity>
);
};
当前,无论使用什么标题或面部,onPress导航都将应用于所有项目。我想介绍一个条件导航。例如,如果
title == 'news'
then onPress...
. Since we can't use if else statements within jsx, I was trying ternary operators:
<TouchableOpacity activeOpacity={0.8}
{title == 'news'? {
onPress={() =>
setShowAddFriendPage(true)
}
} }
/>
But this clearly doesn't work. I get '...' expected.
on title
.
No value exists in scope for the shorthand property 'onPress'. Either declare one or provide an initializer.ts(18004)
on onPress
and
Cannot find name 'setShowAddFriendPage'.
你可以这样