<FastField />

始める前に

<FastField />はパフォーマンスの最適化を目的としています。しかし、本当に必要になるまで使用する必要はありません。ReactのshouldComponentUpdate()の動作に精通している場合のみ、続行してください。警告しておきます。

いいえ、真剣に。続行する前に、公式Reactドキュメントの以下の部分を確認してください。

概要

<FastField />は、大規模なフォーム(約30個以上のフィールド)またはフィールドに非常にコストのかかるバリデーション要件がある場合に使用することを目的とした、<Field />の最適化されたバージョンです。<FastField /><Field>とまったく同じAPIを持っていますが、shouldComponentUpdate()を内部的に実装して、<FastField />の関連部分/Formik状態のスライスへの直接的な更新がない限り、追加のレンダリングをすべてブロックします。

たとえば、<FastField name="firstName" />は、次の場合にのみ再レンダリングされます。

  • values.firstNameerrors.firstNametouched.firstName、またはisSubmittingへの変更。これは浅い比較によって決定されます。注:ドットパスがサポートされています。
  • プロップが<FastField name="firstName" />に追加または削除された場合
  • nameプロップが変更された場合

これらの状況以外では、Formik状態の他の部分が変更されても、<FastField />は再レンダリングされません。ただし、<FastField />によってトリガーされるすべての更新は、他の「標準」<Field />コンポーネントへの再レンダリングをトリガーします。

<FastField />を使用する場合

<Field />がフォーム内の他のすべての<Field />から「独立」している場合、<FastField />を使用できます。.

より具体的には、<Field />が、他の<Field />または<FastField />のFormik状態のスライスへの更新に基づいて動作を変更したり、何かをレンダリングしたりせず、最上位の<Formik />状態(例:isValidatingsubmitCount)の他の部分に依存しない場合、<FastField /><Field />のドロップイン置換として使用できます。

import React from 'react';
import { Formik, Field, FastField, Form } from 'formik';
import * as Yup from 'yup';
const Basic = () => (
<div>
<h1>Sign Up</h1>
<Formik
initialValues={{
firstName: '',
lastName: '',
email: '',
}}
validationSchema={Yup.object().shape({
firstName: Yup.string().required(),
middleInitial: Yup.string(),
lastName: Yup.string().required(),
email: Yup.string().email().required(),
})}
onSubmit={values => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
}, 500);
}}
>
{formikProps => (
<Form>
{/** This <FastField> only updates for changes made to
values.firstName, touched.firstName, errors.firstName */}
<label htmlFor="firstName">First Name</label>
<FastField name="firstName" placeholder="Weezy" />
{/** Updates for all changes because it's from the
top-level formikProps which get all updates */}
{formikProps.touched.firstName && formikProps.errors.firstName && (
<div>{formikProps.errors.firstName}</div>
)}
<label htmlFor="middleInitial">Middle Initial</label>
<FastField name="middleInitial" placeholder="F">
{({ field, form, meta }) => (
<div>
<input {...field} />
{/**
* This updates normally because it's from the same slice of Formik state,
* i.e. path to the object matches the name of this <FastField />
*/}
{meta.touched ? meta.error : null}
{/** This won't ever update since it's coming from
from another <Field>/<FastField>'s (i.e. firstName's) slice */}
{form.touched.firstName && form.errors.firstName
? form.errors.firstName
: null}
{/* This doesn't update either */}
{form.submitCount}
{/* Imperative methods still work as expected */}
<button
type="button"
onClick={form.setFieldValue('middleInitial', 'J')}
>
J
</button>
</div>
)}
</FastField>
{/** Updates for all changes to Formik state
and all changes by all <Field>s and <FastField>s */}
<label htmlFor="lastName">LastName</label>
<Field name="lastName" placeholder="Baby">
{({ field, form, meta }) => (
<div>
<input {...field} />
{/** Works because this is inside
of a <Field/>, which gets all updates */}
{form.touched.firstName && form.errors.firstName
? form.errors.firstName
: null}
</div>
)}
</Field>
{/** Updates for all changes to Formik state and
all changes by all <Field>s and <FastField>s */}
<label htmlFor="email">Email</label>
<Field name="email" placeholder="jane@acme.com" type="email" />
<button type="submit">Submit</button>
</Form>
)}
</Formik>
</div>
);
このページはお役に立ちましたか?

ニュースレターの購読

最新のFormikニュース、記事、リソースをメールで受信します。

Copyright © 2020 Formium, Inc. All rights reserved.