Giter Club home page Giter Club logo

Comments (2)

rturnq avatar rturnq commented on May 27, 2024

I think this is a case of needing more documentation on the matter. As you have found, the route data functions only execute initially when the route is matched and won't run again until that route is un-matched and then matched again. This aligns with Solid's concept of components which similarly only run initially when created.

In order to handle the scenario you have, you'll want to leverage the reactive nature of the params or location properties passed into the function and derive your result. So in this case, instead of creating a signal you could use a memo or even a simple function. If you have async data needs, use a resource with the first argument based on the reactive values.

function FooData({ params }: { params: Params}): Accessor<number> {
  return () => parseInt(params.id)
}
function FooData({ params }: { params: Params}): Accessor<number> {
  return createMemo(() => parseInt(params.id))
}
function FooData({ params }: { params: Params}): { id: number } {
  return {
    get id() {
      return parseInt(params.id)
    }
  }
}
function FooData({ params }: { params: Params}) {
  const [resource] = createResource(() => parseInt(params.id), (id) => /* fetch some data */);
  return resource;
}

from solid-router.

CitrusFruits avatar CitrusFruits commented on May 27, 2024

Got it. Ok yeah, looks like I'm still getting more familiar with the fundamentals of SolidJS. Brushing up on the documentation, this line now makes more sense:

Remember to access signals under a tracking scope if you wish them to react to updates. Tracking scopes are functions that are passed to computations like createEffect or JSX expressions.

If someone else stumbles upon this, I was using createResource in my actual app, but I was not creating a tracking scope for my parameters. So I was doing this:

function FooData({ params }: { params: Params}) {
  const [resource] = createResource(parseInt(params.id), (id) => /* fetch some data */);
  return resource;
}

instead of this

function FooData({ params }: { params: Params}) {
  const [resource] = createResource(() => parseInt(params.id), (id) => /* fetch some data */);
  return resource;
}

Thanks @rturnq!

from solid-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.