Remix表单常用方法有哪些

其他教程   发布日期:2023年11月05日   浏览次数:414

这篇文章主要介绍“Remix表单常用方法有哪些”,在日常操作中,相信很多人在Remix表单常用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Remix表单常用方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    Remix 的三种表单

    • 原生表单

    • Remix 提供的表单组件

    • Remix fetcher 表单

    回顾表单基础

    • 提交行为:enter 按键(只有一个 input type="txt")/使用具有 type=sumbit 的按钮

    • method 不指定时,form 默认使用 get 方法

    • form 提交后默认行为是跳转到 action 对应的页面

    • 表单的提交方式是 content-type = x-www-form-unlencoded

    表单提交的形式

    • 使用 html 标签属性,自动提交

    • 手动提交:钩子函数 sumit 提交方式, fetcher.sumbit 提交方式

    阻止跳转

    通常我们不希望提交表单后发生跳转行为:使用事件阻止函数进行阻止。

    1. const handleClick = (e) => {
    2. e.preventDefault()
    3. }

    Remix 提供的表单组件

    1. import { Form } from "@remix-run/react";

    一个简单的 demo

    1. import { json } from "@remix-run/node";
    2. import { Form } from "@remix-run/react";
    3. export async function action () {
    4. let data = {
    5. a: 'this is data'
    6. }
    7. return json({
    8. ...data
    9. })
    10. }
    11. export default function Index() {
    12. return (
    13. <div>
    14. <div>Remix Form</div>
    15. <Form method="post">
    16. <input type="text" name="a-name-remix"/>
    17. <button type="submit">submit-remix</button>
    18. </Form>
    19. </div>
    20. );
    21. }

    注意:Form 组件没有定义 method 的时候,点击提交按钮没有任何效果。一般添加

    1. method='post'
    。添加之后就可以正常提交 post 请求表单。

    使用钩子函数提交函数

    1. import { json } from "@remix-run/node";
    2. import { Form, useSubmit } from "@remix-run/react";
    3. export async function action () {
    4. let data = {
    5. a: 'this is data'
    6. }
    7. console.log(data)
    8. return json({
    9. ...data
    10. })
    11. }
    12. export default function Index() {
    13. const submit = useSubmit();
    14. const handleClick = (e) => {
    15. e.preventDefault()
    16. submit(e.target, {
    17. method: 'post'
    18. })
    19. }
    20. return (
    21. <div>
    22. <div>Remix Form</div>
    23. <Form onSubmit={handleClick}>
    24. <input type="text" name="a-name-remix"/>
    25. <button type="submit">submit-remix</button>
    26. </Form>
    27. </div>
    28. );
    29. }

    注意手动提交之前先要阻止事件默认行为。

    Remix fetcher 表单

    一个简单的 demo

    1. import { json } from "@remix-run/node";
    2. import { useFetcher } from "@remix-run/react";
    3. export async function action () {
    4. let data = {
    5. a: 'this is data'
    6. }
    7. return json({
    8. ...data
    9. })
    10. }
    11. export default function Index() {
    12. const fetcher = useFetcher();
    13. return (
    14. <div>
    15. <div>Remix Form</div>
    16. <fetcher.Form method="post">
    17. <input type="text" name="a-name-remix"/>
    18. <button type="submit">submit-remix</button>
    19. </fetcher.Form>
    20. </div>
    21. );
    22. }

    Form 添加 post 方法,点击提交按钮,自动提交到当前 Route 模块中的 action 方法中。

    没有定义

    • method 属性

    • action 属性

    没有定义以上两个属性,提交代码的时候,提交函数不会执行

    使用 fetcher.submit 函数提交

    1. import { json } from "@remix-run/node";
    2. import { useFetcher } from "@remix-run/react";
    3. export async function action () {
    4. let data = {
    5. a: 'this is data'
    6. }
    7. console.log(data)
    8. return json({
    9. ...data
    10. })
    11. }
    12. export default function Index() {
    13. const fetcher = useFetcher();
    14. const onSubmit = (e) => {
    15. e.preventDefault();
    16. fetcher.submit(e.target, {
    17. method: 'post',
    18. action: '/main/form'
    19. })
    20. }
    21. return (
    22. <div>
    23. <div>Remix Form</div>
    24. <fetcher.Form onSubmit={onSubmit}>
    25. <input type="text" name="a-name-remix"/>
    26. <button type="submit">submit-remix</button>
    27. </fetcher.Form>
    28. </div>
    29. );
    30. }

    onSubmit 中首先就是要解决提交的默认行为问题,阻止了表单的默认行为之后,使用 submit 方法其实与钩子函数 submit 是一样的。

    useFetcher 的其他内容

    • state 表示当前的条状态

      1. idle/submitting/loading
    • data 表示 action 中响应的数据

    • load 函数表示从路由中读取 action 函数返回的数据

    • submission 是可能构建 optimistic UI

    其他的表单

    • 一个使用 useSubmit 钩子函数手动提交 antd 表单的例子

    1. import { Form, Input, Button } from "antd";
    2. import { useSubmit } from "@remix-run/react";
    3. export async function action() {
    4. return {
    5. a: 1
    6. }
    7. }
    8. export default function () {
    9. const submit = useSubmit();
    10. const handleClick = (data) => {
    11. submit(data, {
    12. method: "post",
    13. });
    14. };
    15. return (
    16. <div>
    17. <Form onFinish={handleClick}>
    18. <Form.Item name="name">
    19. <Input />
    20. </Form.Item>
    21. <Button htmlType="submit" >
    22. 提交
    23. </Button>
    24. </Form>
    25. </div>
    26. );
    27. }
    • 一个手动提交 antd pro-component 表单的例子

    1. import { Button } from "antd";
    2. import { ProForm, ProFormText } from '@ant-design/pro-components'
    3. import { useSubmit } from "@remix-run/react";
    4. export async function action() {
    5. return {
    6. a: 1
    7. }
    8. }
    9. export default function () {
    10. const submit = useSubmit();
    11. const handleClick = async (data: any) => {
    12. submit(data, {
    13. method: "post",
    14. });
    15. return false
    16. };
    17. return (
    18. <div>
    19. <ProForm onFinish={handleClick}>
    20. <ProFormText name="name" />
    21. <Button htmlType="submit" >
    22. 提交
    23. </Button>
    24. </ProForm>
    25. </div>
    26. );
    27. }

    以上就是Remix表单常用方法有哪些的详细内容,更多关于Remix表单常用方法有哪些的资料请关注九品源码其它相关文章!