Reactjs 当应用程序使用react native background任务在后台运行时,如何调用函数?

Reactjs 当应用程序使用react native background任务在后台运行时,如何调用函数?,reactjs,react-native,background-task,Reactjs,React Native,Background Task,当应用程序使用“react native background task”组件在后台运行时,我想调用componentDidMount()或任何其他函数。下面是我想使用的代码,但不知道这是否正确: 从“反应本机后台任务”导入后台任务 BackgroundTask.define(异步()=>{ this.componentDidMount(); BackgroundTask.finish(); }); 导出默认类MyClass扩展组件{ componentDidMount(){ this.some

当应用程序使用“
react native background task
”组件在后台运行时,我想调用
componentDidMount()
或任何其他函数。下面是我想使用的代码,但不知道这是否正确:

从“反应本机后台任务”导入后台任务
BackgroundTask.define(异步()=>{
this.componentDidMount();
BackgroundTask.finish();
});
导出默认类MyClass扩展组件{
componentDidMount(){
this.someTask();
}
}

你能更具体地解释一下吗?如果您想在应用程序启动时进行思考,可以在app.js的componentDidMount()中进行

或:

BackgroundTask.define(()=>{
log('Hello from a background task')
BackgroundTask.finish()
})
类MyApp扩展了React.Component{
componentDidMount(){
BackgroundTask.schedule()
}
render(){
返回你好世界
}
}

我刚刚发现,当应用程序的状态发生变化时,我可以使用AppState调用函数。那不是更好的解决办法吗?事实上,我不确定哪一个更好。
componentDidMount() {
    this.job();
}

async job(){
     BackgroundTask.define(() => {
       console.log('Hello from a background task')
       BackgroundTask.finish()
     })
}
BackgroundTask.define(() => {
  console.log('Hello from a background task')
  BackgroundTask.finish()
})

class MyApp extends React.Component {
  componentDidMount() {
    BackgroundTask.schedule()
  }

  render() {
    return <Text>Hello world</Text>
  }
}