亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

歸納總結(jié)Remix?表單常用方法及示例詳解

 更新時(shí)間:2023年03月24日 11:38:01   作者:?jiǎn)讨蝊x  
這篇文章主要為大家歸納總結(jié)了Remix?表單常用方法及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

Remix 的三種表單

  • 原生表單
  • Remix 提供的表單組件
  • Remix fetcher 表單

回顧表單基礎(chǔ)

  • 提交行為:enter 按鍵(只有一個(gè) input type="txt")/使用具有 type=sumbit 的按鈕
  • method 不指定時(shí),form 默認(rèn)使用 get 方法
  • form 提交后默認(rèn)行為是跳轉(zhuǎn)到 action 對(duì)應(yīng)的頁面
  • 表單的提交方式是 content-type = x-www-form-unlencoded

表單提交的形式

  • 使用 html 標(biāo)簽屬性,自動(dòng)提交
  • 手動(dòng)提交:鉤子函數(shù) sumit 提交方式, fetcher.sumbit 提交方式

阻止跳轉(zhuǎn)

通常我們不希望提交表單后發(fā)生跳轉(zhuǎn)行為:使用事件阻止函數(shù)進(jìn)行阻止。

const handleClick = (e) => {
 e.preventDefault()
}

Remix 提供的表單組件

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

一個(gè)簡(jiǎn)單的 demo

import { json } from "@remix-run/node";
import { Form } from "@remix-run/react";
export async function action () {
  let data = {
    a: 'this is data'
  }
  return json({
    ...data
  })
}
export default function Index() {
  return (
    <div>
      <div>Remix Form</div>
      <Form method="post">
        <input type="text" name="a-name-remix"/>
        <button type="submit">submit-remix</button>
      </Form>
    </div>
  );
}

注意:Form 組件沒有定義 method 的時(shí)候,點(diǎn)擊提交按鈕沒有任何效果。一般添加 method='post'。添加之后就可以正常提交 post 請(qǐng)求表單。

使用鉤子函數(shù)提交函數(shù)

import { json } from "@remix-run/node";
import { Form, useSubmit } from "@remix-run/react";
export async function action () {
  let data = {
    a: 'this is data'
  }
  console.log(data)
  return json({
    ...data
  })
}
export default function Index() {
  const submit = useSubmit();
  const handleClick = (e) => {
    e.preventDefault()
    submit(e.target, {
      method: 'post'
    })
  }
  return (
    <div>
      <div>Remix Form</div>
      <Form onSubmit={handleClick}>
        <input type="text" name="a-name-remix"/>
        <button type="submit">submit-remix</button>
      </Form>
    </div>
  );
}

注意手動(dòng)提交之前先要阻止事件默認(rèn)行為。

Remix fetcher 表單

一個(gè)簡(jiǎn)單的 demo

import { json } from "@remix-run/node";
import { useFetcher } from "@remix-run/react";
export async function action () {
  let data = {
    a: 'this is data'
  }
  return json({
    ...data
  })
}
export default function Index() {
  const fetcher = useFetcher();
  return (
    <div>
      <div>Remix Form</div>
      <fetcher.Form method="post">
        <input type="text" name="a-name-remix"/>
        <button type="submit">submit-remix</button>
      </fetcher.Form>
    </div>
  );
}

Form 添加 post 方法,點(diǎn)擊提交按鈕,自動(dòng)提交到當(dāng)前 Route 模塊中的 action 方法中。

沒有定義

  • method 屬性
  • action 屬性

沒有定義以上兩個(gè)屬性,提交代碼的時(shí)候,提交函數(shù)不會(huì)執(zhí)行

使用 fetcher.submit 函數(shù)提交

import { json } from "@remix-run/node";
import { useFetcher } from "@remix-run/react";
export async function action () {
  let data = {
    a: 'this is data'
  }
  console.log(data)
  return json({
    ...data
  })
}
export default function Index() {
  const fetcher = useFetcher();
  const onSubmit = (e) => {
    e.preventDefault();
    fetcher.submit(e.target, {
      method: 'post',
      action: '/main/form'
    })
  }
  return (
    <div>
      <div>Remix Form</div>
      <fetcher.Form onSubmit={onSubmit}>
        <input type="text" name="a-name-remix"/>
        <button type="submit">submit-remix</button>
      </fetcher.Form>
    </div>
  );
}

onSubmit 中首先就是要解決提交的默認(rèn)行為問題,阻止了表單的默認(rèn)行為之后,使用 submit 方法其實(shí)與鉤子函數(shù) submit 是一樣的。

useFetcher 的其他內(nèi)容

  • state 表示當(dāng)前的條狀態(tài) idle/submitting/loading
  • data 表示 action 中響應(yīng)的數(shù)據(jù)
  • load 函數(shù)表示從路由中讀取 action 函數(shù)返回的數(shù)據(jù)
  • submission 是可能構(gòu)建 optimistic UI

其他的表單

  • 一個(gè)使用 useSubmit 鉤子函數(shù)手動(dòng)提交 antd 表單的例子
import { Form, Input, Button } from "antd";
import { useSubmit } from "@remix-run/react";
export async function action() {
  return {
    a: 1
  }
}
export default function () {
  const submit = useSubmit();
  const handleClick = (data) => {
    submit(data, {
      method: "post",
    });
  };
  return (
    <div>
      <Form onFinish={handleClick}>
        <Form.Item name="name">
          <Input />
        </Form.Item>
        <Button htmlType="submit" >
          提交
        </Button>
      </Form>
    </div>
  );
}
  • 一個(gè)手動(dòng)提交 antd pro-component 表單的例子
import { Button } from "antd";
import { ProForm, ProFormText } from '@ant-design/pro-components'
import { useSubmit } from "@remix-run/react";
export async function action() {
  return {
    a: 1
  }
}
export default function () {
  const submit = useSubmit();
  const handleClick = async (data: any) => {
    submit(data, {
      method: "post",
    });
    return false
  };
  return (
    <div>
      <ProForm onFinish={handleClick}>
        <ProFormText name="name" />
        <Button htmlType="submit" >
          提交
        </Button>
      </ProForm>
    </div>
  );
}

小結(jié)

回顧的表單的默認(rèn)行為,以及在 Remix 提供的表單能力 Form/fetcher.Form。手動(dòng)提交以及自動(dòng)管理的兩種方式。其次在 antd 系統(tǒng)的表單中使用 useSubmit 手動(dòng)提交鉤子函數(shù)。大概講到了 Remix 中使用了各種表單行為。更多關(guān)于Remix 表單用法的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 對(duì)react中間件的理解

    對(duì)react中間件的理解

    這篇文章主要介紹了對(duì)react中間件的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • react-native 封裝選擇彈出框示例(試用ios&android)

    react-native 封裝選擇彈出框示例(試用ios&android)

    本篇文章主要介紹了react-native 封裝選擇彈出框示例(試用ios&android),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • React State與生命周期詳細(xì)介紹

    React State與生命周期詳細(xì)介紹

    React將組件(component)看成一個(gè)狀態(tài)機(jī)(State Machines),通過其內(nèi)部自定義的狀態(tài)(State)和生命周期(Lifecycle)實(shí)現(xiàn)并與用戶交互,維持組件的不同狀態(tài)
    2022-08-08
  • 詳解React Native頂|底部導(dǎo)航使用小技巧

    詳解React Native頂|底部導(dǎo)航使用小技巧

    本篇文章主要介紹了詳解React Native頂|底部導(dǎo)航使用小技巧 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • React中多語言的配置方式

    React中多語言的配置方式

    這篇文章主要介紹了React中多語言的配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 無廢話快速上手React路由開發(fā)

    無廢話快速上手React路由開發(fā)

    本文以簡(jiǎn)潔為目標(biāo),幫助快速上手react-router-dom默認(rèn)你接觸過路由相關(guān)的開發(fā),通過實(shí)例代碼講解的很詳細(xì),對(duì)React路由相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-05-05
  • React中遍歷數(shù)組生成標(biāo)簽問題

    React中遍歷數(shù)組生成標(biāo)簽問題

    這篇文章主要介紹了React中遍歷數(shù)組生成標(biāo)簽問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • React組件的使用詳細(xì)講解

    React組件的使用詳細(xì)講解

    React組件分為函數(shù)組件與class組件;函數(shù)組件是無狀態(tài)組件,class稱為類組件;函數(shù)組件只有props,沒有自己的私有數(shù)據(jù)和生命周期函數(shù);class組件有自己私有數(shù)據(jù)(this.state)和生命周期函數(shù)
    2022-11-11
  • React中super()和super(props)的區(qū)別小結(jié)

    React中super()和super(props)的區(qū)別小結(jié)

    本文主要介紹了React中super()和super(props)的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • React高階組件使用教程詳解

    React高階組件使用教程詳解

    高階組件就是接受一個(gè)組件作為參數(shù)并返回一個(gè)新組件(功能增強(qiáng)的組件)的函數(shù)。這里需要注意高階組件是一個(gè)函數(shù),并不是組件,這一點(diǎn)一定要注意,本文給大家分享React 高階組件HOC使用小結(jié),一起看看吧
    2022-12-12

最新評(píng)論