我想在listview reaact native中单击按钮后更改按钮图像, 我想用api中的数据作为按钮,我喜欢在加载页面中更改按钮,并且工作良好,但是我单击按钮api就像从数据库中添加或删除一样,但是不更改图像。
changeLike(id,text)
{
var formDat = new FormData();
formDat.append('type', text);
formDat.append('fav', id);
formDat.append('number', '0');
return fetch('xxx', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: formDat
})
.then(response => response.json())
.then(responseJson => {
if(responseJson.result[0].credit ==1)
{
//i want to change my image here
//i use this.filterImage(id) but is not work
}
}) .catch(error => {
Alert.alert(error);
});
}
onlikeClick(id)
{
for(let i=0;i<this.state.favs.length;i++)
{
if(this.state.favs[i].id == id)
{ return this.changeLike(id,'remove')
}
}
{return this.changeLike(id,'add')
}
}
filterImage(text){
for(let i=0;i<this.state.favs.length;i++)
{
if(this.state.favs[i].id == text)
return <Image source={require('./../../assets/Image/like1.png')}
style={{width:20,height:20,marginLeft:3}} resizeMode='contain' />
}
return <Image source={require('./../../assets/Image/like.png')}
style={{width:20,height:20,marginLeft:3}} resizeMode='contain' />
}