前提・実現したいこと
appstate の状態が変化するたびに 呼び出した、
BackgroundTimer を nextAppState === 'active' の時のみとめたいです。
BackgroundTimer.setTimeout() → BackgroundTimer.clearTimeout()
発生している問題・エラーメッセージ
nextAppState === 'active' の時のみ clearTimeout を実現したいのですが、できません。
該当のソースコード
reactnative
1class test extends React.Component { 2 componentDidMount() { 3 AppState.addEventListener('change', this._handleAppStateChange); 4 } 5 componentWillUnmount() { 6 AppState.removeEventListener('change', this._handleAppStateChange); 7 } 8 9 _handleAppStateChange = nextAppState => { 10 console.log(AppState); 11 12 const backTimer = BackgroundTimer.setTimeout(() => { 13 console.log('1分'); 14 }, 60000); 15 16 //動くがどの appstate の場合も止まる 17 // const stopTimer = BackgroundTimer.clearTimeout(backTimer); 18 // BackgroundTimer.clearTimeout(backTimer); 19 20 //BackgroundTimer.clearTimeout(backTimer); が機能しない 21 // const stopTimer = nextAppState === 'active' 22 // ? BackgroundTimer.clearTimeout(backTimer) 23 // : console.log('non active'); 24 25 if (nextAppState === 'active') { 26 //BackgroundTimer.clearTimeout(backTimer); //この位置だと機能しない 27 console.log('Foregound mode'); 28 } 29 30 if (nextAppState === 'inactive') { 31 console.log('Inactive mode'); 32 } 33 34 if (nextAppState === 'background') { 35 console.log('Backgound mode'); 36 } 37 };
試したこと
BackgroundTimer.clearTimeout(backTimer); が機能しない
※1 の位置に console.log() は出力される
const stopTimer = nextAppState === 'active' ? BackgroundTimer.clearTimeout(backTimer) //※1 : console.log('non active');
この位置だと機能しない
if (nextAppState === 'active') { BackgroundTimer.clearTimeout(backTimer); //この位置だと機能しない console.log('Foregound mode'); }
補足情報(FW/ツールのバージョンなど)
react-native-background-timer:2.3.1

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/19 04:58
2020/08/19 05:02
2020/08/19 05:13