-1
Good evening, I’m starting in React and have the following line:
class Namelist extends React.Component{ constructor(props){ super(props); firebase.initializeApp(config);
this.state = {
clientes: []
};
}
componentDidMount(){
this.getUserData();
}
getUserData = () => {
let ref = firebase.database().ref('/');
ref.on('value', snapshot => {
const state = snapshot.val();
this.setState(state);
console.log(state);
});
console.log("Dados Recebidos !")
};
render() {
const { clientes } = this.state;
return(
<IonList>
{clientes.map((item,index) =>
<IonItem key={index}>
<IonLabel>
<p>Nome:</p>
<h2>{item.Name}</h2>
<p>Horário:</p>
<h2>{item.Horario}</h2>
</IonLabel>
<IonLabel></IonLabel>
<IonLabel>
<IonButton color="success"><IonIcon icon={checkmark} /></IonButton>
<IonButton color="danger"><IonIcon icon={trash} /></IonButton>
</IonLabel>
</IonItem>
)}
</IonList>
);
}
}
What I’m not getting is just filling in the state which has the array of clients, to redenrize a list. It’s all functional, the data is coming however the {clients} = this.state is not filled with them.
What you have in your
snapshot.val()
?– Rafael Tavares
comes the data from firebase, exactly like this: customers: [{name: 'Test', schedule: 'test'}]
– José Jorge
If you use another variable name to store this, does it work? (ex:
const newState = await ...
and thenthis.setState(newState)
) Because if the value is really right, I don’t see any problems beyond you creating a variable with the same name asstate
.– Rafael Tavares
I’ll try here, I’ll get back to you soon, but thanks anyway !
– José Jorge