diff --git a/src/useConstant.js b/src/useConstant.js deleted file mode 100644 index 929be24..0000000 --- a/src/useConstant.js +++ /dev/null @@ -1,22 +0,0 @@ -// @flow -import React from 'react' - -/** - * A simple hook to create a constant value that lives for - * the lifetime of the component. - * - * Plagiarized from https://github.com/Andarist/use-constant - * - * Do NOT reuse this code unless you know what you're doing. - * Use Andarist's hook; it's more fault tolerant to things like - * falsy values. - * - * @param {Function} init - A function to generate the value - */ -export default function useConstant(init: () => T): T { - const ref = React.useRef() - if (!ref.current) { - ref.current = init() - } - return ref.current -} diff --git a/src/useFieldArray.js b/src/useFieldArray.js index 6cf256a..b03504b 100644 --- a/src/useFieldArray.js +++ b/src/useFieldArray.js @@ -1,12 +1,11 @@ // @flow -import { useMemo } from 'react'; +import { useCallback, useMemo } from 'react' import { useForm, useField } from 'react-final-form' import { fieldSubscriptionItems, ARRAY_ERROR } from 'final-form' import type { Mutators } from 'final-form-arrays' import type { FieldValidator, FieldSubscription } from 'final-form' import type { FieldArrayRenderProps, UseFieldArrayConfig } from './types' import defaultIsEqual from './defaultIsEqual' -import useConstant from './useConstant' const all: FieldSubscription = fieldSubscriptionItems.reduce((result, key) => { result[key] = true @@ -40,8 +39,8 @@ const useFieldArray = ( }, {} ), [name, formMutators]) - const validate: FieldValidator = useConstant( - () => (value, allValues, meta) => { + const validate: FieldValidator = useCallback( + (value, allValues, meta) => { if (!validateProp) return undefined const error = validateProp(value, allValues, meta) if (!error || Array.isArray(error)) { @@ -52,7 +51,8 @@ const useFieldArray = ( ;((arrayError: any): Object)[ARRAY_ERROR] = error return arrayError } - } + }, + [validateProp] ) const {

AltStyle によって変換されたページ (->オリジナル) /