'React Hook effect how to add events
Hello it´s my first time asking a question here. I´m currently learning react. I would like to know what I´m doing wrong. I want to have a response that it says there´s no event today in the following code if there´s no events that day. Can Anybody help me?
function getHitos(){
const url = `${API_HOST}/tal-dias`;
return fetch(url).then((response)=>{
return response.json();
}).then((result)=>{
return result;
});
}
export default function LeerMas(props) {
// constante donde se guarda la id, que hemos pasado por parametros desde tal día
const {id} = props.route.params
console.log(id);
const [hitos, setHitos] = useState(null);
var diaMes = new Date()
useEffect(() => {
getHitos().then((response)=>{
setHitos(response);
//console.log(talDia);
})
}, []);
return (
<ScrollView style={styles.fondo}>
{map(hitos,(hito)=>{
// mostrar solo el tal día que coincida con la ida pasada por parametros
if(hito.id===id){
return(
<SafeAreaView key={hito.id} style={styles.margenes}>
<View style={styles.containerFecha}>
<Text style={styles.fechaAnio}>{moment(hito.Fecha).format("YYYY")}</Text>
<Text style={styles.fechaDia}>{moment(diaMes).format("DD")+" "+moment(diaMes).format("MMM")}</Text>
</View>
<View style={styles.containerImg}>
{map(hito.Imagen,(img)=>{
return(
<Image key={img.id} style={[styles.imgPequeña]} source={{
uri: API_HOST+img.url
}}/>
)
})
}
</View>
<Text style={styles.titulo}>{hito.Titulo}</Text>
<View style={styles.opacidad}>
<Markdown style={styles}>
{hito.Descripcion}
</Markdown>
</View>
<Text
style = {styles.autor}
onPress={()=> Linking.openURL('https://twitter.com/'+hito.Autores.Twitter)}>
Publicado por: {hito.Autores.Nombre}
</Text>
</SafeAreaView>
)
}
})}
</ScrollView>
)
}
import React, {useState, useEffect} from 'react'
import { SafeAreaView, StyleSheet, Text, View, Image } from 'react-native'
import {map, filter, truncate} from "lodash"
import { API_HOST } from '../utils/host'
import moment from "moment"
import Markdown from 'react-native-markdown-display'
import 'moment/locale/es';
function getTalDia() {
const url = `${API_HOST}/tal-dias`;
return fetch(url).then((response)=>{
return response.json();
}).then((result)=>{
return result;
});
}
export default function TalDiaComponent(props) {
/*
UTILIZAMOS EL HOOK useEstate PARA CREAR UNA VARIABLE INTERNA Y ALMACENAR EL ESTADO DE UN COMPONENE
EN ESTE CASO DONDE ALMACENAREMOS LA INFORMACION DE TALDIA
*/
const [talDia, setTalDia] = useState(null);
const fechaActual = new Date();
/*
UTILIZAMOS EL HOOK useEffect PARA INICIALIZAR VARIABLES O APIs.
*/
useEffect(() => {
getTalDia().then((response)=>{
console.log('Entra en la respuesta');
console.log(response);
console.log('despues de la respuesta');
if (response.length > 0) {
console.log('Si hay tal dia');
setTalDia(response)
}else{
//Vista de 'No hay elementos que mostrar'
console.log('no hay tal dia') ;
}
})
}, []);
useEffect(()=>{
//API Call
}, []);
// sacar la fecha y mes del dia en que nos encontramos
var fechaHoy =moment().format("MM-DD");
// filtrar el contenido de tal dia por la fecha de hoy
var filtro=filter(talDia,function(persona) {
return moment(persona.Fecha).format("MM-DD")===fechaHoy;
});
// mostrar el contenido que coincida con el dia y mes en el que nos encontramos
function mostrarInfo(props){
const {navigation} = props
return(
map(filtro,(noticias)=>{
return(
<SafeAreaView key={noticias.id} style={styles.margenes}>
<Text style={styles.linea}></Text>
<View
style={styles.container}
onStartShouldSetResponder={()=>navigation.navigate("Tal día como hoy...",{id:noticias.id})}
accessibilityRole="button"
>
<Text style={styles.fecha}>{moment(noticias.Fecha).format("YYYY")}</Text>
<View style={styles.containerInterno}>
<Text style={[styles.titulo]}>{noticias.Titulo}</Text>
<View style={styles.opacidad}>
<Markdown style={styles}>
{
truncate(noticias.Descripcion,{
'length':200,
'omission':'...'
})
}
</Markdown>
</View>
<View style={styles.containerImg}>
{map(noticias.Imagen,(img)=>{
return(
<Image key={img.id} style={[styles.img]} source={{
uri: API_HOST+img.url
}}/>
)
})
}
</View>
</View>
</View>
</SafeAreaView>
)
})
)
}
//console.log(talDia);
return (
<SafeAreaView>
<Text style={styles.cabecera}>Tal día como hoy...</Text>
<Text style={styles.fechaActual}>{moment(fechaActual).format("DD")+" "+moment(fechaActual).format("MMM")}</Text>
{mostrarInfo(props)}
</SafeAreaView>
)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
