0
The useEffect hook fires when some variable in your array refreshes. However, when the value of the variable remains the same, the hook simply ignores.
This hook gets a value and a push on the array in useState.
useEffect(() => {
setDataUser(dataUser => [...dataUser, afterAttackUser]);
}, [afterAttackUser]);
But if the value of afterAttackUser
is 4, for example, and receiving 4 again, it’s as if the value of the variable did not change, so useEffect does not fire.. But I need to put this value inside the array...
Any light to proceed? I created some logics but unsuccessfully.
Edit: Well I want to render the values of the array in a map that will resume a Text.
entire component:
export default function CombatLog() {
const afterAttackUser = useSelector(state => state.combatReducer.afterAttack);
const currentEnemyLife = useSelector(state => state.currentEnemyInfoReducer.currentLife);
const maxEnemyLife = useSelector(state => state.currentEnemyInfoReducer.maxLife);
const [dataUser, setDataUser] = useState(Array);
useEffect(() => {
setDataUser(dataUser => [...dataUser, afterAttackUser]);
console.log(`tamanho do array: ${dataUser.length}`);
}, [afterAttackUser]);
return(
<ImageBackground style={styles.container} resizeMode='stretch' source={Img}>
<Text style={styles.title}>Registro do Combate</Text>
<View>
{
dataUser.map((damage, index) => {
if(damage != 1)
return <Text style={styles.textUser} key={index}>Você infligiu {damage} de dano</Text>
})
}
</View>
</ImageBackground>
);
}
you have how to put where you update, only
useEffect
I can’t tell why this, I mean, put the whole component !– novic
explain also the desired functioning in your question speaks speaks and does not explain (understand we want to reach the same point but, lack information)
– novic
was bad, I just updated brother
– Nycollas
Friend, following the variable is a certain Contextapi or Redux: it only suffers change upon some event, Cade the event, the function something like that? doesn’t update at all.
– novic
It’s in another file, and it’s shooting great. the problem is that the useEffect does not fire when the value of the variable remains the same, I just need a logic to get past that. I’ve tried countless times..
– Nycollas
So, how this useEffect works as you mentioned, will only run when the value is changed what you intend to do maybe the solution is another (almost sure)
– novic
You cannot update at Redux/context level even?
– Ricardo Pedroni