【发布时间】:2021-03-17 17:49:34
【问题描述】:
我在导出默认值 withUrqlClient(createUrqlClient)(ChangePassword) 的 (ChangePassword) 处收到此错误:
'FunctionComponent & { getInitialProps?(context: NextPageContext): { token: string; } |承诺; }' 不可分配给类型为“NextComponentType
”的参数。 输入 'FunctionComponent & { getInitialProps?(context: NextPageContext): { token: string; } |承诺; }' 不可分配给类型 'FunctionComponent & { getInitialProps?(context: PartialNextContext): {} |承诺; }'。 输入 'FunctionComponent & { getInitialProps?(context: NextPageContext): { token: string; } |承诺; }' 不可分配给类型 'FunctionComponent'。 参数 'props' 和 'props' 的类型不兼容。 类型 'PropsWithChildren' 不可分配给类型 'PropsWithChildren'。 “PropsWithChildren”类型中缺少属性“token”,但类型“{ token: string; }'.ts(2345)
代码如下:
import { NextPage } from "next";
import { Wrapper } from "../../components/Wrapper";
import { Formik, Form } from "formik";
import { toErrorMap } from "../../utils/toErrorMap";
import { InputField } from "../../components/InputField";
import { Box, Button, Link, Flex } from "@chakra-ui/react";
import { useChangePasswordMutation } from "../../generated/graphql";
import { useRouter } from "next/router";
import { withUrqlClient } from "next-urql";
import { createUrqlClient } from "../../utils/createUrqlClient";
import NextLink from "next/link";
const ChangePassword: NextPage<{ token: string }> = ({ token }) => {
const router = useRouter();
const [, changePassword] = useChangePasswordMutation();
const [tokenError, setTokenError] = useState("");
return (
<Wrapper variant="small">
<Formik
initialValues={{ newPassword: "" }}
onSubmit={async (values, { setErrors }) => {
const response = await changePassword({
newPassword: values.newPassword,
token,
});
if (response.data?.changePassword.errors) {
const errorMap = toErrorMap(response.data.changePassword.errors);
if ("token" in errorMap) {
setTokenError(errorMap.token);
}
setErrors(errorMap);
} else if (response.data?.changePassword.user) {
// worked
router.push("/");
}
}}
>
{({ isSubmitting }) => (
<Form>
<InputField
name="newPassword"
placeholder="new password"
label="New Password"
type="password"
/>
{tokenError ? (
<Flex>
<Box mr={2} style={{ color: "red" }}>
{tokenError}
</Box>
<NextLink href="/forgot-password">
<Link>click here to get a new password</Link>
</NextLink>
</Flex>
) : null}
<Button
mt={4}
type="submit"
isLoading={isSubmitting}
variantColor="teal"
>
change password
</Button>
</Form>
)}
</Formik>
</Wrapper>
);
};
ChangePassword.getInitialProps = ({ query }) => {
return {
token: query.token as string,
};
};
export default withUrqlClient(createUrqlClient)(ChangePassword);
【问题讨论】:
标签: reactjs typescript next.js change-password urql