如何在QWIK中创建一个不可序列化的函数来避免自动序列化?

How can I create a non-serializable function in QWIK that avoids automatic serialization?

提问人:David Pino 提问时间:8/29/2023 更新时间:8/29/2023 访问量:135

问:

由于可访问性原因,我需要防止某些函数在 QWIK 中被序列化。在下载过程中对这些函数进行序列化会导致不可接受的延迟。如何在QWIK中有效地创建这些不可序列化的函数,同时确保它们保持其性能和功能?

export const ProductFinder = component$(() => {
  const product = useSignal<string>("")
  const nav = useNavigate();
  useStylesScoped$(styles);

  const handleSearch = $(async () => {
    if (product.value.length !== 0) await nav(`/?page=1&categoryType=NEW&filter=${product.value}`);
  })

  return (
    <div class="form">
      <input type="text" class="input" placeholder="Buscar productos" value={product.value} onInput$={(event) => (product.value = (event.target as HTMLInputElement).value)} />
      <button class="button" onClick$={handleSearch} aria-label='Search'>
        <SearchNormalIcon size='100%' />
      </button>
    </div>
  )
});
JavaScript 打字稿 qwik

评论


答:

0赞 Harsh Mangalam 8/29/2023 #1

您可以通过提供 type 然后使用 function 使任何值和函数在 qwik 中不可序列化。NoSerializenoSerialize()

import { component$, noSerialize, type NoSerialize, useStore } from '@builder.io/qwik';

type Props = {
  someMethod: NoSerialize<() => void>;
}
export const ProductFinder = component$(({someMethod}:Props) => {
  const product = useSignal<string>("")
  const someObj = useSignal<NoSerialize<any>>()
  const nav = useNavigate();
  useStylesScoped$(styles);

  const handleSearch = $(async () => {
    noSerialize(() => console.log("some log"));
    someObj.value = noSerialize(some value)
    if (product.value.length !== 0) await nav(`/?page=1&categoryType=NEW&filter=${product.value}`);
  })

  return (
    <div class="form">
      <input type="text" class="input" placeholder="Buscar productos" value={product.value} onInput$={(event) => (product.value = (event.target as HTMLInputElement).value)} />
      <button class="button" onClick$={handleSearch} aria-label='Search'>
        <SearchNormalIcon size='100%' />
      </button>
    </div>
  )
});