connect()

connect() 是一個高階元件 (HoC),允許您將任何東西掛鉤到 Formik 的上下文。它在內部用於構建 <Field><Form>,但您可以根據需要使用它來構建新的元件。

類型簽章

connect<OuterProps, Values = any>(Comp: React.ComponentType<OuterProps & { formik: FormikContext<Values> }>) => React.ComponentType<OuterProps>

範例

import React from 'react';
import { connect, getIn } from 'formik';
// This component renders an error message if a field has
// an error and it's already been touched.
const ErrorMessage = props => {
// All FormikProps available on props.formik!
const error = getIn(props.formik.errors, props.name);
const touch = getIn(props.formik.touched, props.name);
return touch && error ? error : null;
};
export default connect(ErrorMessage);
這個頁面有幫助嗎?

訂閱我們的電子報

最新的 Formik 新聞、文章和資源,將會發送到您的收件匣。

版權所有 © 2020 Formium, Inc. 保留所有權利。