Вот мой код, и я пытаюсь перечислить пользовательские ответы, лайки и неприязни, но всякий раз, когда я нажимаю «нравится» или «не нравится», значение элемента не меняется.Я хочу изменить только определенное значение элемента, а не весь список.
class Test extends Component {
constructor(props) {
super(props);
this.state = {
replayList: [],
replayMsg: ""
};
this.doReplayList();
}
static navigationOptions = {
title: "Replay"
};
Это список воспроизведения.
doReplayList() {
var formData = new FormData();
formData.append("cmt_ref", 1185);
formData.append("pageno", 1);
let postData = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data"
},
body: formData
};
fetch(Api.commentReplyList, postData)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
const STATUS = responseJson.STATUS;
if (STATUS == "FAIL") {
console.log(responseJson.MESSAGES);
} else {
this.setState({
replayList: responseJson.DATA
});
}
})
.catch(error => {
console.error(error);
});
}
Здесь я называю api.
doLikePost(cmt_data) {
var formData = new FormData();
formData.append("like_lgn_id", 172);
formData.append("like_post_id", cmt_data.cmt_post_id);
formData.append("like_cmt_id", cmt_data.cmt_id);
formData.append("like_type", "1");
console.log(formData);
let postData = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data"
},
body: formData
};
fetch(Api.likeUnlikePost, postData)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
const STATUS = responseJson.STATUS;
if (STATUS == "FAIL") {
console.log(responseJson.MESSAGES);
} else {
this.setState = {
replayList: responseJson.DATA.goodcount
};
console.log(responseJson.MESSAGES);
}
})
.catch(error => {
console.error(error);
});
}
Здесь я называю неприязнь api.
doDisLikePost(cmt_data) {
var formData = new FormData();
formData.append("like_lgn_id", 172);
formData.append("like_post_id", cmt_data.cmt_post_id);
formData.append("like_cmt_id", cmt_data.cmt_id);
formData.append("like_type", "2");
let postData = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "multipart/form-data"
},
body: formData
};
fetch(Api.likeUnlikePost, postData)
.then(response => response.json())
.then(responseJson => {
const STATUS = responseJson.STATUS;
if (STATUS == "FAIL") {
console.log(responseJson.MESSAGES);
} else {
console.log(responseJson.MESSAGES);
}
})
.catch(error => {
console.error(error);
});
}
Здесь я устанавливаю пользовательский вид и устанавливаю данные ответа.
renderItem = ({ item }) => {
return(
<View
style={[{flex: 1,borderBottomColor: Color.colorLine,borderBottomWidth:
2,marginBottom: 15}]}>
<View
style={[styles.row,{justifyContent: "space-between",alignItems: "flex-
start",alignContent: "flex-start",marginTop: 5,marginBottom: 5}]}>
<Image
resizeMethod="scale"
resizeMode="contain"
source={{ uri: item.usr_image }}
style={[styles.icon,{alignSelf: "flex-start",width: 50,height:
50,borderWidth: 1,marginTop: 10,marginRight: 20,marginLeft: 30}]}/>
<View
style={[styles.column,{ alignItems: "flex-start", flex: 1,
marginTop: 10 }]}>
<Text
style={{fontWeight: "bold",fontSize: 12,color:
Color.UserNameColor}}>
{item.usr_name}
</Text>
<View style={styles.row}>
<Text style={{ fontSize: 12, color: Color.colorPost }}>
12/12/18
</Text>
</View>
<Text style={{ fontSize: 12, color: Color.colorPost }}>
{item.cmt_msg}
</Text>
</View>
</View>
<View
style={[styles.row,{justifyContent: "space-around",marginTop:
5,marginBottom: 10,marginRight: 20,marginLeft: 20}]}>
<TouchableOpacity onPress={() => this.doLikePost(item)}>
<View style={styles.row}>
<Image
source={Icon.ic_like}
style={[styles.icon, { margin: 5 }]}
/>
<Text style={{ marginLeft: 5 }}>({item.goodcount})</Text>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.doDisLikePost(item)}>
<View style={styles.row}>
<Image
source={Icon.ic_dislike}
style={[styles.icon, { margin: 5 }]}
/>
<Text style={{ marginLeft: 5 }}>({item.badcount})</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
};
render() {
return (
<View style={[styles.container, { backgroundColor: Color.white }]}>
<FlatList data={this.state.replayList} renderItem={this.renderItem} />
<View
style={{flexDirection: "row",justifyContent:
"center",alignItems: "center",margin: 5}}>
<View
style={{flex: 1,marginRight: 5,borderColor:
"gray",borderRadius: 5,borderWidth: 1}}
>
<TextInput
placeholder={"Write a Replay"}
underlineColorAndroid="transparent"
onChangeText={value => this.setState({ replayMsg: value
})}
style={{padding: 5,marginLeft: 5,fontSize:
12,textAlignVertical: "center"}}
/>
</View>
<TouchableOpacity onPress={this.doReplay.bind(this)}>
<View
style={{elevation: 10,width: 50,height: 50,borderRadius:
30,backgroundColor: Color.white,overflow: "visible",alignItems:
"center",justifyContent: "center"}}>
<Image
source={Icon.send}
style={[styles.icon,{ width: 30, height: 30,
justifyContent: "center" }]}/>
</View>
</TouchableOpacity>
</View>
</View>
);
}
}
export default Test;