小编Dra*_*era的帖子

如何使用 Typescript 以 React hook 形式一次设置多个值

我有以下界面:

export interface DataResponse {
  user_id: string;
  name: string;
  phone_number: string;
  country: string;
}
Run Code Online (Sandbox Code Playgroud)

它用作通过反应查询获取数据的类型。如果有数据,我正在使用 useEffect 填充字段:

useEffect(() => {
    if (userData) {
      Object.entries(userData).forEach(
        ([name, value]) => setValue(name, value));
    }
}, [setValue, userData]);
Run Code Online (Sandbox Code Playgroud)

userData 的类型是 DataResponse...当我用它设置值时,setState(name, value)它会在名称下划线并引发以下错误:

Argument of type 'string' is not assignable to parameter of type | "name" | "phone_number" | "country"
Run Code Online (Sandbox Code Playgroud)

typescript reactjs react-hook-form

21
推荐指数
1
解决办法
3万
查看次数

使用 axios 拦截器时如何修复 config.headers.Authorization“对象可能未定义”

我得到以下代码:

loggedInAxios.interceptors.request.use(
  async (config) => {
    if (isTokenExpired('access_token')) {
      const response = await getRefreshToken();
      await refreshAccessToken(response);
    }
    const accessToken = localStorage.getItem('access_token');
    config.headers.Authorization = `Bearer ${accessToken}`;
    return config;
  },
  (error) => error
);
Run Code Online (Sandbox Code Playgroud)

但打字稿抱怨 config.headers.Authorization 对象可能未定义。

我通过添加以下内容找到了一种方法:

if (!config) {
 config = {};
}
if (!config.headers) {
  config.headers = {};
}
Run Code Online (Sandbox Code Playgroud)

但我不认为这是最好的方法......

typescript axios

12
推荐指数
2
解决办法
2万
查看次数

React 中的模板字符串

我得到以下代码:

export default function CancelledPayment() {

  const linkPage = <Link to="/dashboard/payment" className={clsx(classes.paymentLinkTypography, classes.link)}> here </Link>;

  return (
    <Container>
      <Paper >
        <Paper />
        <Typography>
          {` To go back to your order please click ${linkPage}.You will be redirect in ${count} seconds.`}
        </Typography>
      </Paper>
    </Container>
  );
}
Run Code Online (Sandbox Code Playgroud)

知道为什么它返回 linkPage 作为 [object Object] 吗?计数器是正确的,一切正常,只是这个链接页面不正常。如果我把它拿出来:

To go back to your order please click {linkPage}. {`You will be redirect in ${count} seconds.`}
Run Code Online (Sandbox Code Playgroud)

它工作正常,在其他一些情况下也是如此,但我希望所有内容都在一行中,使用模板字符串。

javascript reactjs

4
推荐指数
1
解决办法
4350
查看次数

如何覆盖主题中的材质 ui 阴影

import {
  createMuiTheme, responsiveFontSizes,
} from "@material-ui/core/styles";

let theme = createMuiTheme({
  palette: {
    primary: {
      main: "#000",
    },
    secondary: {
      main: "#ccc",
    },
  },
  typography: {
    fontFamily: "Roboto",
  },
  shadows: [
    "none",
    "0px 15px 60px rgba(0, 0, 0, 0.25)",
    "0px 35px 60px rgba(0, 0, 0, 0.25)",
    "20px 55px 60px rgba(0, 0, 0, 0.25)",
    "10px 15px 60px rgba(0, 0, 0, 0.25)",
  ],
});

theme = responsiveFontSizes(theme);

export default theme;
Run Code Online (Sandbox Code Playgroud)

控制台中有一条警告:警告:失败的道具类型:Material-UI:此高度4未在组件中实现。既然它是一个包含 25 个元素的数组,应该怎么做呢?

reactjs material-ui

4
推荐指数
1
解决办法
4535
查看次数

Stripe PaymentElement 的集成。警告 不支持的属性更改:options.clientSecret 不是可变属性?

我有 PaymentMethodPage,如下所示:

import React, { FC, useState } from 'react';
import { Elements } from '@stripe/react-stripe-js';
import { loadStripe } from '@stripe/stripe-js';
import {
  getClientSecret,
} from '../../api/PaymentCalls/PaymentCalls';
import PaymentMethodSetupForm from './PaymentMethodSetupForm/PaymentMethodSetupForm';
import AddPaymentMethodContainer from './AddPaymentMethodContainer/AddPaymentMethodContainer';
import {
  ClientSecretResponse,
} from '../../interfaces/paymentMethod.interface';


const stripePromise = loadStripe(
  process.env.REACT_APP_STRIPE_PUBLIC_KEY as string
);

const PaymentMethodPage: FC = () => {
  const [enableAddPaymentMethod, setEnableAddPaymentMethod] =
    useState<boolean>(false);

  const {
    data: clientSecretKeyData,
  }: UseQueryResult<ClientSecretResponse, ExtendedError> = useQuery<
    ClientSecretResponse,
    ExtendedError
  >('getClientSecret', getClientSecret, {
    retry: false,
    refetchOnWindowFocus: false, …
Run Code Online (Sandbox Code Playgroud)

javascript stripe-payments typescript reactjs

1
推荐指数
1
解决办法
3779
查看次数