tengo este problema al querer recargar el arreglo desde otra pantalla,
Pantalla del detalle donde al agregar un comentario se actualiza esta pantalla, pero solamente se actualiza ésta. En cambio a la pantalla de Home no se actualiza.
HomeDetalle:
state = {
Post: {
PostId: 0,
PostComment: '',
PostedBy: '',
ImageId: 0,
PostedOn: new Date(),
UserId: 0,
Comments: [] as PostComment[],
LikesCount: 0,
},
textoComentario: '',
onPostRefresh: (post: Post) => {},
};
constructor(props: Props) {
super(props);
this.state = {
Post: props.route.params.Post,
textoComentario: '',
onPostRefresh: props.route.params.onPostRefresh,
};
}
formatDate(input: Date): string {
return moment(input).locale('es').startOf('hour').fromNow()
}
async PublicarComentario() {
const muroService = new MuroService();
const postId = this.state.Post.PostId;
const model: PublicarComentario = {
PostId: postId,
Comment: this.state.textoComentario
}
const response = await muroService.PublicarComentario(model);
if(!response.OperationSuccesful) {
Alert.alert(response.ErrorMessage);
} else {
Alert.alert("Haz publicado un comentario");
}
this.setState({
Post: response.Post,
textoComentario: '',
});
}
goBack() {
this.props.navigation.goBack();
this.state.onPostRefresh(this.state.Post);
}
Aquí tengo mi funcion para actualizar mi componente llamado Posts en la pantalla
Home:
actualizarPost(post: Post) {
const Post = this.state.Posts.findIndex(e => e.PostId === post.PostId);
let postCopy = [...this.state.Posts];
postCopy[Post] = {...this.state.Posts[Post]}
this.setState({
Posts: postCopy.concat(post)
})
}
Ya revisé debuggeando que funciona todo bien, el post en el actualizarPost()
funciona de manera correcta, se trae el post actualizado pero al momento de querer hacer un setState no me funciona, alguien tiene alguna idea de que puedo hacer?
Comparto el .map
en pantalla Home:
{
this.state.Posts.map((post: Post, index:number) => (
<PostComponent navigation={this.props.navigation} Post={ post } onPostSelected={ this.onPostSelected } key={ post.PostId + '' } ></PostComponent>
))
}
En mi componente PostComponent
tengo esto funcionando de manera correcta:
<TouchableOpacity onPress={() => { this.props.onPostSelected(this.props.Post); }
Tambien tengo esto en mi pantalla Home para mandar en la navegacion un onPostRefresh con la funcion this.actualizarPost para que regrese con el Post actualizado:
onPostSelected(post: Post) {
this.props.navigation.navigate("HomeDetalle", { Post: post, onPostRefresh: this.actualizarPost } );
}
Two Children
, también deje otros comentarios, saludos.this.state.Posts
en el componenteHome
? Supongo que estas usandoComponentDidMount -> newMuroService
.Home
, no queda claro cómo se pasa dehome
aHomeDetalle
, ni cómo se disparaPublicarComentario
enHomeDetalle
, saludos.