createResource 抛出错误后 createMemo 停止更新

createMemo stops updating after createResource throws an error

createResource 抛出时,依赖于 createResourcecreateMemo 在进一步交互后停止更新。

这个例子展示了当输入 3 时抛出的情况。现在我该如何恢复,以便进一步查询将像往常一样更新 m_id

我怀疑我需要在某个地方放一个 ErrorBoundary 但没有这种情况的例子。

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

你可以这样做来捕获错误

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

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

API 文档:https://www.solidjs.com/docs/latest/api#onerror 游乐场:https://playground.solidjs.com/?hash=1457545275&version=1.3.16