#Derived value in event handler causing router error

7 messages · Page 1 of 1 (latest)

paper cave
#

Error: Make sure your app is wrapped in a <Router /> when calling an eventhandler. Just trying to log a derived signal/prop in my buy function. The values from product() get printed correctly in my DOM

  const product = () => props.product
  const [qty, setQty] = createSignal(0)

  function buy() {
    console.log(product())
    // console.log(qty())
  }

The second console.log works fine. But the first throws

Uncaught Error: Make sure your app is wrapped in a <Router />
    at invariant (utils.js?v=8381e9f0:28:15)
    at useRouter (routing.js?v=8381e9f0:9:32)
    at useRoute (routing.js?v=8381e9f0:11:75)
    at useParams (routing.js?v=8381e9f0:37:32)
    at product (ProductDetail.tsx:11:22)
paper cave
#

Ok so wrapping it in createMemo fixed it. const product = createMemo(() => props.product). Is this the correct way to do this?

quick swan
#

i don't understand the reason for the error so I can't help with that but why do you need a derived signal for that? why not just use props?

paper cave
quick swan
#

@paper cave