last day (15 days later) » 

11:47 AM
0
A: How can I set some functions to run while the AppState is inactive in react-native

SiSaYou should add and remove AppState change listener in componentDidMount and componentWillUnmount: state = {lastAppState: AppState.currentState}; componentDidMount() { AppState.addEventListener('change', this.handleAppStateChange); } componentWillUnmount() { AppState.removeEventListener...

 
it does not work
handleAppStateChange(nextAppState){ if(this.state.lastAppState === 'active' && nextAppState === 'inactive'){ addIncome().then().catch(error => {}) } this.setState({lastAppState: nextAppState}) }
can you help me?
 
@VictorZarzu did you define state = {lastAppState: ''}; ?
I edited my answer and added it before componentDidMount. check it again
 
yeah. Can you see me entire code?
I mean the functions that are in connection with my problem
 
@VictorZarzu what is wrong now? do you get error?
 
I have the state in the constructor
no. the function is not working. if I put a button it works
 
11:47 AM
@VictorZarzu you mean it never calls the function?!
 
I think so. I want to add some funds when it close the app to test if it works. then I will add an if and something else. but when I put a button with that function in works. the function in the inactive mode is not called
I want that the function to be called while the app is in inactive mode
are you there?=)
 
@VictorZarzu it should call handleAppStateChange whenever your app state change, now!
 
and how I can do that?
I want to be called when the date is the one that I choose
 
@VictorZarzu is function calls now?! and I don't understand your last question!
 
I just want that the app to call a function while the app is in background. I want to add some amount to a var from the database if the day of the new Date is the same with another one
 
11:47 AM
@VictorZarzu I have anydesk
 
export const addIncome = () => new Promise((resolve, reject) => { Realm.open(databaseOptions) .then(realm => { realm.write(() => { const nowDate = new Date() let updatingProfile = realm.objectForPrimaryKey('Profile', 1) if(nowDate.getMinutes() == 55 && nowDate.getSeconds() === 0){ updatingProfile.funds += updatingProfile.incomeAmount realm.create('Profile', updatingProfile, true) } }) }).catch(error => reject(error)) })
this is the function I want to be called while the app is in background mode
and I added this in componentWillUnmount: AppState.addEventListener('background', () => addIncome().then().catch(error => {}))
 
did you read my answer?! you should AppState.addEventListener('change', this.handleAppStateChange); in componentDidMount and write your code for change database value in // your app state changes to inactive part that I write in my answer
 
componentDidMount() { AppState.addEventListener('change', this.handleAppStateChange) } componentWillUnmount(){ AppState.removeEventListener('change', this.handleAppStateChange) } handleAppStateChange = nextAppState => { this.setState({appState: nextAppState}) }else{ while(this.state.appState === 'inactive'){ addIncome().then().catch(error => {}) } } }
I've done this thing in my project
 
12:16 PM
are you there?=)
 

last day (15 days later) »