Home > front end >  createMemo stops updating after createResource throws an error
createMemo stops updating after createResource throws an error

Time:05-31

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.

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

CodePudding user response:

You can do something like this to catch errors

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

  one rror(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

  • Related