首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性'token‘在'PropsWithChildren<WithUrqlProps>’类型中缺失,但在类型'{ token: string;}‘中是必需的

属性'token‘在'PropsWithChildren<WithUrqlProps>’类型中缺失,但在类型'{ token: string;}‘中是必需的
EN

Stack Overflow用户
提问于 2021-03-17 17:49:34
回答 3查看 1K关注 0票数 2

在导出默认withUrqlClient(createUrqlClient)(ChangePassword):中,我在(ChangePassword)处得到这个错误

类型为‘

:FunctionComponent<{ token: string;}> &{getInitialProps?&{getInitialProps?(上下文: NextPageContext):{getInitialProps: string;} Promise<{ token: string;}>;}’}‘的FunctionComponent<参数不能分配给类型'NextComponentType’‘的参数。键入'FunctionComponent<{ token: string;}> &{getInitialProps?&{getInitialProps?(上下文: NextPageContext):{令牌: string;}{ Promise<{ token: string;}>;}‘’不能分配到键入'FunctionComponent &{getInitialProps?(上下文: PartialNextContext):{} Promise<{}>;}‘。键入'FunctionComponent<{ token: string;}> &{getInitialProps?&{getInitialProps?(上下文: NextPageContext):{getInitialProps:NextPageContext;} Promise<{ token: string;}>;}‘’不能分配到键入'FunctionComponent‘。参数的类型‘道具’和‘道具’是不相容的。类型'PropsWithChildren‘不能分配到键入'PropsWithChildren<{ token: string;}>’。属性'token‘在'PropsWithChildren’类型中缺失,但在类型'{ token: string;}'.ts(2345)中是必需的。

以下是代码:

代码语言:javascript
复制
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);
EN

回答 3

Stack Overflow用户

发布于 2021-03-21 10:10:05

我有一个能解决你类型问题的快速解决方法

代码语言:javascript
复制
export default withUrqlClient(createUrqlClient)(ChangePassword as any);

通过将changePassword类型转换为任何类型,它将在编译时获得类型,并且它不会显示任何升温。

(注:如果您按此操作,将不会自动完成或键入)

票数 2
EN

Stack Overflow用户

发布于 2021-04-23 10:18:13

我可以用另一种方式打字,我希望这是正确的方法。我的做法是:

代码语言:javascript
复制
export default withUrqlClient(createUrlClient)(
  ChangePassword as FunctionComponent<
    PropsWithChildren<WithUrqlProps | { token: string }>
  >
);

其中类型:

  • FunctionComponentPropsWithChildren从"react"
  • WithUrqlProps导入,从"next-urql"

导入。

票数 1
EN

Stack Overflow用户

发布于 2021-06-08 20:54:07

您可以从useRouter而不是getInitialProps获取查询令牌。

代码语言:javascript
复制
import { Box, Button, Flex, Link } from "@chakra-ui/react";
import { Form, Formik } from "formik";
import { withUrqlClient } from "next-urql";
import { useRouter } from "next/router";
import React, { useState } from "react";
import InputField from "../../components/InputField";
import Wrapper from "../../components/Wrapper";
import { useChangePasswordMutation } from "../../generated/graphql";
import { createUrqlClient } from "../../utils/createUrqlClient";
import { toErrorMap } from "../../utils/toErrorMap";
import NextLink from "next/link";

const ChangePassword = () => {
  const [, changePassword] = useChangePasswordMutation();
  const [tokenError, setTokenError] = useState("");

  const router = useRouter();
  const { token } = router.query;

  return (
    <Wrapper variant="small">
      <Formik
        initialValues={{ newPassword: "" }}
        onSubmit={async (values, { setErrors }) => {
          const response = await changePassword({
            newPassword: values.newPassword,
            token: token as string,
          });
          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) {
            router.push("/");
          }
        }}
      >
        {({ isSubmitting }) => (
          <Form>
            <InputField
              name="newPassword"
              placeholder="new password"
              label="New Password"
              type="password"
            />
            {tokenError ? (
              <Flex>
                <Box mr={2} color="red">
                  {tokenError}
                </Box>
                <NextLink href="/forgot-password">
                  <Link>click here to get a new one</Link>
                </NextLink>
              </Flex>
            ) : null}
            <Box mt={4}>
              <Button type="submit" isLoading={isSubmitting} colorScheme="teal">
                change password
              </Button>
            </Box>
          </Form>
        )}
      </Formik>
    </Wrapper>
  );
};

export default withUrqlClient(createUrqlClient)(ChangePassword);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66678567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档