Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SolidJS - How to trigger createEffect using an external dependency?

Is there a way to trigger Solid's createEffect using an external dependency, as with React's useEffect dependency array?

I want to call setShowMenu on location.pathname change.

const location = useLocation()

createEffect(() => {
    console.log(location.pathname) // << external dependency
    setShowMenu(false)
})

Until there's a better option, I'm using this as a workaround.

const location = useLocation()

createEffect(() => location.pathname && setShowMenu(false))
like image 246
Ryan Prentiss Avatar asked Oct 29 '25 09:10

Ryan Prentiss


1 Answers

Perhaps you are looking for the on helper?

createEffect(
  on(
    () => location.pathname,
    () => setShowMenu(false)
  )
);

https://www.solidjs.com/docs/latest/api#on

like image 159
FoolsWisdom Avatar answered Oct 31 '25 00:10

FoolsWisdom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!