No, function will not be accessible outside of useEffect
. Regular Javascript rules apply.
Why would you not define it outside of useEffect
?
const foo = () => { console.log('yay') };
useEffect(() => {
foo();
}, [])
It is also not rally clear what everytime i refresh the page
means in the context of react-native
, could you elaborate?
There is another way to "trigger" useEffect
, to call it indirectly:
function C {
[t, setT] = useState();
useEffect(() => {
const foo = () => console.log(t);
foo();
}, [t])
return <Button onPress={() => setT(new Date())} title="Trigger foo" />
}
Above useEffect
will be called at least every time t
changes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…