【发布时间】:2020-11-19 18:14:16
【问题描述】:
我试图通过 React Hooks 的 if 条件进行异步等待。 下面的代码是在我尝试将其变为 React Hooks 之前:
async startService() {
if (Platform.OS !== 'android') {
console.log('Only Android platform is supported');
return;
}
if (Platform.Version >= 26) {
const channelConfig = {
id: 'ForegroundServiceChannel',
name: 'Notification Channel',
description: 'Notification Channel for Foreground Service',
enableVibration: true,
importance: 2
};
await VIForegroundService.createNotificationChannel(channelConfig);
}
}
我正试图将它变成 React Hooks
useEffect(() => {
async function startService() {
if (Platform.OS !== 'android') {
console.log('Only Android platform is supported');
return;
}
if (Platform.Version >= 26) {
const channelConfig = {
id: 'ForegroundServiceChannel',
name: 'Notification Channel',
description: 'Notification Channel for Foreground Service',
enableVibration: false,
importance: 2
};
await VIForegroundService.createNotificationChannel(channelConfig);
}
const notificationConfig = {
id: 3456,
title: 'Foreground Service',
text: 'Foreground service is running',
icon: 'ic_notification',
priority: 0
};
if (Platform.Version >= 26) {
notificationConfig.channelId = 'ForegroundServiceChannel';
}
await VIForegroundService.startService(notificationConfig);
}startService();
}, []);
我也试图在我的 jsx 中这样调用它:
<Button onPress={() => this.startService()}>
<Text>Tes</Text>
</Button>
还是不行,是不是我写错了?
【问题讨论】:
标签: react-native if-statement async-await react-hooks