提交表单时更改并从 useActionData() 获取实时更新的内容。

huangapple go评论99阅读模式
英文:

Remix submit form onChange and get live update from useActionData()

问题

I am trying to create a remix page where i want to get live preview when i change a Select tag. It is going through a Form since i want it to translate the data.

A solution to my problem could be to make preventDefault(first code example) to work or to find a way to make useActionData()(second code example) to work when using useFetcher.

With this code, the preventDefault does not work. I need preventDefault to work so that the selector doesn't reset. I could make the action function also send the name so that the DefaultValue in select is imported from action function, but this is going to become a big form and i dont want to refresh the whole page.

英文:

I am trying to create a remix page where i want to get live preview when i change a Select tag. It is going through a Form since i want it to translate the data.

A solution to my problem could be to make preventDefault(first code example) to work or to find a way to make useActionData()(second code example) to work when using useFetcher

With this code, the preventDefault does not work. I need preventDefault to work so that the selector doesn't reset. I could make the action function also send the name so that the DefaultValue in select is imported from action function, but this is going to become a big form and i dont want to refresh the whole page.

  1. export async function action({ request }: ActionArgs) {
  2. const form = new URLSearchParams(await request.text())
  3. const name = form.get('name');
  4. console.log('name', name);
  5. return json({ message: name ? `Hello ${name}!` : 'Select name' });
  6. }
  7. export default function Page() {
  8. const actionData = useActionData();
  9. console.log('actionData', actionData);
  10. return (
  11. <>
  12. <Form method="post">
  13. <h1>{actionData ? actionData.message : 'Select name.'}</h1>
  14. <select
  15. name="name"
  16. onChange={e => {
  17. e.preventDefault();
  18. e.target.form.submit();
  19. }}
  20. >
  21. <option value="name 1">Name 1</option>
  22. <option value="name 2">Name 2</option>
  23. <option value="name 3">Name 3</option>
  24. </select>
  25. <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
  26. Submit
  27. </button>
  28. </Form>
  29. </>
  30. );
  31. }

I have also tried following https://github.com/remix-run/remix/issues/1807 using useRef and useFetcher, but even tho the action function runs, it gives me undefined when logging actionData.

  1. export async function action({ request }: ActionArgs) {
  2. const form = new URLSearchParams(await request.text())
  3. const name = form.get('name');
  4. console.log('name', name);
  5. return json({ message: name ? `Hello ${name}!` : 'Select name' });
  6. }
  7. export default function Page() {
  8. const actionData = useActionData();
  9. console.log('actionData', actionData); // Gives undefined
  10. const formRef = useRef<HTMLFormElement>(null);
  11. const fetcher = useFetcher();
  12. return (
  13. <>
  14. <fetcher.Form method="post" ref={formRef}>
  15. <h1>{actionData ? actionData.message : 'Select name.'}</h1>
  16. <select
  17. name="name"
  18. onChange={() => {
  19. fetcher.submit(formRef.current);
  20. }}
  21. >
  22. <option value="name 1">Name 1</option>
  23. <option value="name 2">Name 2</option>
  24. <option value="name 3">Name 3</option>
  25. </select>
  26. <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
  27. Submit
  28. </button>
  29. </fetcher.Form >
  30. </>
  31. );
  32. }

答案1

得分: 0

以下是您要翻译的内容:

Main file test.tsx:

  1. import { useFetcher } from '@remix-run/react';
  2. export default function Page() {
  3. const fetcher = useFetcher();
  4. return (
  5. <>
  6. <div>
  7. <h1>{fetcher.data ? fetcher.data.message : 'Select name.'}</h1>
  8. <select
  9. name="name"
  10. onChange={e => {
  11. fetcher.load(`loader/?name=${e.target.value}`);
  12. }}
  13. >
  14. <option value="name 1">Name 1</option>
  15. <option value="name 2">Name 2</option>
  16. <option value="name 3">Name 3</option>
  17. </select>
  18. <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
  19. Submit
  20. </button>
  21. </div>
  22. </>
  23. );
  24. }

Loaderfile loader.ts:

  1. import type { LoaderArgs } from '@remix-run/node';
  2. import { json } from '@remix-run/node';
  3. export async function loader({ request }: LoaderArgs) {
  4. const form = new URLSearchParams(await request.text());
  5. const name = form.get('name');
  6. return json({ message: name ? `Hello ${name}!` : 'Select name' });
  7. }

请注意,我已经移除了代码部分并返回了翻译好的内容。

英文:

I found a solution, it was to use fetcher.load, and then have it as a own loader file.

Main file test.tsx:

  1. import { useFetcher } from &#39;@remix-run/react&#39;;
  2. export default function Page() {
  3. const fetcher = useFetcher();
  4. return (
  5. &lt;&gt;
  6. &lt;div&gt;
  7. &lt;h1&gt;{fetcher.data ? fetcher.data.message : &#39;Select name.&#39;}&lt;/h1&gt;
  8. &lt;select
  9. name=&quot;name&quot;
  10. onChange={e =&gt; {
  11. fetcher.load(`loader/?name=${e.target.value}`);
  12. }}
  13. &gt;
  14. &lt;option value=&quot;name 1&quot;&gt;Name 1&lt;/option&gt;
  15. &lt;option value=&quot;name 2&quot;&gt;Name 2&lt;/option&gt;
  16. &lt;option value=&quot;name 3&quot;&gt;Name 3&lt;/option&gt;
  17. &lt;/select&gt;
  18. &lt;button type=&quot;submit&quot; className=&quot;bg-blue-500 text-white font-bold py-2 px-4&quot;&gt;
  19. Submit
  20. &lt;/button&gt;
  21. &lt;/div&gt;
  22. &lt;/&gt;
  23. );
  24. }

Loaderfile loader.ts:

  1. import type { LoaderArgs } from &#39;@remix-run/node&#39;;
  2. import { json } from &#39;@remix-run/node&#39;;
  3. export async function loader({ request }: LoaderArgs) {
  4. const form = new URLSearchParams(await request.text());
  5. const name = form.get(&#39;name&#39;);
  6. return json({ message: name ? `Hello ${name}!` : &#39;Select name&#39; });
  7. }

huangapple
  • 本文由 发表于 2023年4月6日 19:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定