Next.js Discord

Discord Forum

Function on variable change

Answered
WhyFencePost (Ping Reply) posted this in #help-forum
Open in Discord
Is there a next event listener for onVariableChange, so that I can run a function every time a specific variable changes?
Answered by riský
yeah useEffect ALSO if you make a function and make it as input, the function should just rerun too

i hope this helps, but i havent tested and made on discord:
const [a, setA] = useState()
function fn(v) {
  return `hi: ${v}`
}

// `yes` should be updated on a's change
const yes = fn(a)

return <button onClick{(() => setA(Math.random()))} />
View full answer

2 Replies

yeah useEffect ALSO if you make a function and make it as input, the function should just rerun too

i hope this helps, but i havent tested and made on discord:
const [a, setA] = useState()
function fn(v) {
  return `hi: ${v}`
}

// `yes` should be updated on a's change
const yes = fn(a)

return <button onClick{(() => setA(Math.random()))} />
Answer