I have a list and everytime a child is added on firebase I want to get that child and add it to the list but for some reason it is adding the same child twice.
const [list, setList] = useState([{name:"Raja",age:5,count:10,dineIn:false,takeOut:true,black:false,white:true,asian:false,hispanic:false,other:false,male:true,female:false,zipcode:60645},
{name:"Mano",age:5,count:10,dineIn:false,takeOut:true,black:false,white:true,asian:false,hispanic:false,other:false,male:true,female:false,zipcode:60645},
{name:"Tom",age:5,count:10,dineIn:false,takeOut:true,black:false,white:true,asian:false,hispanic:false,other:false,male:true,female:false,zipcode:60645},]);
const addToList = (newItem) => {
setList((prevList) => [...prevList, newItem]);
};
useEffect(() => {
onChildAdded(userRef,(data) =>{
addToList(data.exportVal());
})
}, [])
ext
I tried using an if statment to check if the data passed into childadded is different from the pervious but that didn't work.
I tried simply just changing a variable to the new data and then useEffect to check if the data is different and then add it.