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

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

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.

export async function action({ request }: ActionArgs) {
    const form = new URLSearchParams(await request.text())
    const name = form.get('name');
    console.log('name', name);
    return json({ message: name ? `Hello ${name}!` : 'Select name' });
}

export default function Page() {
    const actionData = useActionData();
    console.log('actionData', actionData);

    return (
        <>
            <Form method="post">

                <h1>{actionData ? actionData.message : 'Select name.'}</h1>

                <select 
                    name="name" 
                    onChange={e => {
                        e.preventDefault();
                        e.target.form.submit();
                    }}
                >
                    <option value="name 1">Name 1</option>
                    <option value="name 2">Name 2</option>
                    <option value="name 3">Name 3</option>
                </select>

                <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
                    Submit
                </button>

                </Form>
        </>
    );
}

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.

export async function action({ request }: ActionArgs) {
    const form = new URLSearchParams(await request.text())
    const name = form.get('name');
    console.log('name', name);
    return json({ message: name ? `Hello ${name}!` : 'Select name' });
}

export default function Page() {
    const actionData = useActionData();
    console.log('actionData', actionData); // Gives undefined

    const formRef = useRef<HTMLFormElement>(null);
    const fetcher = useFetcher();

    return (
        <>
            <fetcher.Form method="post" ref={formRef}>

                <h1>{actionData ? actionData.message : 'Select name.'}</h1>

                <select 
                    name="name" 
                    onChange={() => {
                        fetcher.submit(formRef.current);
                    }}
                >
                    <option value="name 1">Name 1</option>
                    <option value="name 2">Name 2</option>
                    <option value="name 3">Name 3</option>
                </select>

                <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
                    Submit
                </button>

            </fetcher.Form >
        </>
    );
}

答案1

得分: 0

以下是您要翻译的内容:

Main file test.tsx:

import { useFetcher } from '@remix-run/react';

export default function Page() {
    const fetcher = useFetcher();
    return (
        <>
            <div>
                <h1>{fetcher.data ? fetcher.data.message : 'Select name.'}</h1>

                <select
                    name="name"
                    onChange={e => {
                        fetcher.load(`loader/?name=${e.target.value}`);
                    }} 
                >
                    <option value="name 1">Name 1</option>
                    <option value="name 2">Name 2</option>
                    <option value="name 3">Name 3</option>
                </select>

                <button type="submit" className="bg-blue-500 text-white font-bold py-2 px-4">
                    Submit
                </button>
            </div>
        </>
    );
}

Loaderfile loader.ts:

import type { LoaderArgs } from '@remix-run/node';
import { json } from '@remix-run/node';

export async function loader({ request }: LoaderArgs) {
    const form = new URLSearchParams(await request.text());
    const name = form.get('name');
    return json({ message: name ? `Hello ${name}!` : 'Select name' });
}

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

英文:

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

Main file test.tsx:

import { useFetcher } from &#39;@remix-run/react&#39;;

export default function Page() {
    const fetcher = useFetcher();
	return (
		&lt;&gt;
			&lt;div&gt;
				&lt;h1&gt;{fetcher.data ? fetcher.data.message : &#39;Select name.&#39;}&lt;/h1&gt;

				&lt;select
					name=&quot;name&quot;
					onChange={e =&gt; {
						fetcher.load(`loader/?name=${e.target.value}`);
					}} 
				&gt;
					&lt;option value=&quot;name 1&quot;&gt;Name 1&lt;/option&gt;
					&lt;option value=&quot;name 2&quot;&gt;Name 2&lt;/option&gt;
					&lt;option value=&quot;name 3&quot;&gt;Name 3&lt;/option&gt;
				&lt;/select&gt;

				&lt;button type=&quot;submit&quot; className=&quot;bg-blue-500 text-white font-bold py-2 px-4&quot;&gt;
					Submit
				&lt;/button&gt;
			&lt;/div&gt;
		&lt;/&gt;
	);
}

Loaderfile loader.ts:

import type { LoaderArgs } from &#39;@remix-run/node&#39;;
import { json } from &#39;@remix-run/node&#39;;

export async function loader({ request }: LoaderArgs) {
	const form = new URLSearchParams(await request.text());
	const name = form.get(&#39;name&#39;);
	return json({ message: name ? `Hello ${name}!` : &#39;Select name&#39; });
}

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:

确定