【发布时间】:2022-11-04 16:28:48
【问题描述】:
我现在正在尝试使用 Formik 在 NextJs 13 (Typescript) 上创建一个表单。我创建的表单不起作用,然后我尝试从Formik 添加示例代码 sn-ps,如下所示。我创建的表单和 Formik 的示例都只在控制台中返回 TypeError: React.createContext is not a function。我可以在另一行控制台错误中看到这个(sc_server)/./node_modules/formik/dist/formik.cjs.development.js。
import * as React from 'react';
import {
Formik,
FormikHelpers,
FormikProps,
Form,
Field,
FieldProps,
} from 'formik';
interface MyFormValues {
firstName: string;
}
export const MyApp: React.FC<{}> = () => {
const initialValues: MyFormValues = { firstName: '' };
return (
<div>
<h1>My Example</h1>
<Formik
initialValues={initialValues}
onSubmit={(values, actions) => {
console.log({ values, actions });
alert(JSON.stringify(values, null, 2));
actions.setSubmitting(false);
}}
>
<Form>
<label htmlFor="firstName">First Name</label>
<Field id="firstName" name="firstName" placeholder="First Name" />
<button type="submit">Submit</button>
</Form>
</Formik>
</div>
);
};
我在 /app/page.tsx 中导入了上述组件,如下所示。
import { MyApp } from '../components/form/MyApp';
export default function Home() {
return (
<div>
<MyApp />
</div>
);
}
【问题讨论】:
标签: reactjs typescript next.js formik