Fetching documentation…
morphEffect registers a side effect that runs after render and re-runs when dependencies change.
import { morphEffect, morphState } from 'morph'
export default function App() {
const [count, setCount] = morphState(0)
morphEffect(() => {
console.log("Count changed:", count)
})
return (
<body>
<div>Count: {count}</div>
<button onClick={() => setCount(count + 1)}>+1</button>
</body>
)
}The effect runs once after the initial render, then re-runs every time count changes.
Pass a dependency array as the second argument:
morphEffect(() => {
console.log("name or age changed:", name, age)
}, [name, age])[] — runs once after initial render onlyReturn a cleanup function from the effect:
morphEffect(() => {
const timer = setInterval(() => {
console.log("tick")
}, 1000)
return () => {
clearInterval(timer)
}
}, [])The cleanup runs before the effect re-runs and when the component unmounts.
morphEffect(() => {
console.log("State:", { count, name, active })
})morphEffect(() => {
fetchInitialData()
}, []) // empty deps = run oncemorphEffect(() => {
if (userId) {
loadUserProfile(userId)
}
}, [userId]) // only re-runs when userId changesmorphEffect(() => {
const id = setInterval(poll, 5000)
return () => clearInterval(id)
}, [])create_effect in the C++ runtime (runtime/cpp/reactivity/signal.h)