Issue
I have got a function and need only once call it, when the suer make login.
function Read(){
const [counter, setCounter] = useState(1);
const starCountRef = ref(db, 'users/' + user.localId);
onValue(starCountRef, (snapshot) => {
const data = snapshot.val();
setCounter(data.countTrains);
});
}
useEffect(() => {
Read();
console.log('counter:', counter)
},[]);
I need to write data here into counter variable
Solution
I am not sure what you want to achieve, here is your function
const functionThatIsCalledOnlyOnce = ( counterValue ) => {
setCounter(counterValue)
}
When you want to call this function? if you want to call it only once, when app open put it in useEffect:
useEffect(()=>{
functionThatIsCalledOnlyOnce(1);
},[]);
Answered By - Alija Fajic
Answer Checked By - - Marilyn (ReactFix Volunteer)