Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

createMemo stops updating after createResource throws an error

When createResource throws, a createMemo that depends on createResource stops updating after further interaction.

This examples shows the case, when input 3 it throws. Now How do I recover so further queries will update the m_id as usual.

I suspect I need to put an ErrorBoundary somewhere but there is no example of this case.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { createMemo, createEffect, createSignal, createResource } from "solid-js";
import { render } from "solid-js/web";

const fetchUser = async (id) => { if (id == 3) throw 3; else return id; }

const App = () => {
  const [userId, setUserId] = createSignal();
  const [user] = createResource(userId, fetchUser);

  let m_id = createMemo(() => {
      return user()
  })

createEffect(() => {
    console.log(m_id())
})

  return (
    <>
      <input
        type="number"
        min="1"
        placeholder="Enter Numeric Id"
        onInput={(e) => setUserId(e.currentTarget.value)}
      />
      <span>{user.error && "Error..."}</span>
      <div>
        <pre>{JSON.stringify(user(), null, 2)}</pre>
      </div>
    </>
  );
};

render(App, document.getElementById("app"));

>Solution :

You can do something like this to catch errors

  const [user] = createResource(userId, fetchUser);

  onError(err => {
    console.warn(err)
  })

API doc: https://www.solidjs.com/docs/latest/api#onerror
Playground: https://playground.solidjs.com/?hash=1457545275&version=1.3.16

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading