For Rest Api => React Native Todo App Rest Api
react-native run-android
react-native-start
For virtual device after app installation click Reload(R,R) or double press "R" key on your keyboard.
For real device shake the phone and click Reload
Send notes to rest api
addNote() {
if(this.state.noteText){
var d = new Date();
fetch('https://androidtodoapp.herokuapp.com/saveNote' , {
method : 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body : JSON.stringify({
date : d.getFullYear() + "/" + (d.getMonth() +1) + "/" + d.getDate(),
note : this.state.noteText
})
})
.then((response) => {
if(response.status == 200)
{
this.state.noteArray.push({'date': d.getFullYear() + "/" + (d.getMonth() +1) + "/" + d.getDate() , 'note': this.state.noteText});
this.setState({noteArray : this.state.noteArray});
this.setState({noteText : ''});
}
})
.catch((error) => {
});
}
}Get notes from rest api
getNotes(){
fetch('http://androidtodoapp.herokuapp.com/getNotes' , {
method : 'POST',
headers : {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((responseData) => {
for(var i = 0; i < responseData.length; i++)
{
this.state.noteArray.push({'date' : responseData[i].date , 'note' : responseData[i].note});
this.setState({noteArray : this.state.noteArray});
this.setState({noteText : ''});
}
})
.catch((error) => {
Alert.alert(error.toString());
})
}Fill notes to view
for(var i = 0; i < responseData.length; i++)
{
this.state.noteArray.push({'date' : responseData[i].date , 'note' : responseData[i].note});
this.setState({noteArray : this.state.noteArray});
this.setState({noteText : ''});
}
