Search code examples
javascriptreactjstypescriptsolid-js

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))

Solution

  • Perhaps you are looking for the on helper?

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

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