【问题标题】:Error in react-spring with typescript for reactreact-spring 与 typescript 的反应错误
【发布时间】:2021-08-05 08:10:52
【问题描述】:

我正在尝试使用 react-spring 制作动画,但是当我去调用 useTransition 函数时,传递了一些我不理解的错误的属性

import React from 'react';
import { useTransition } from 'react-spring';
import { Container } from './styles';
import { ToastMessage } from '../../hooks/toast';
import Toast from './Toast/index';

interface ToastContainerProps {
  messages: ToastMessage[];
}

const ToastContainer: React.FC<ToastContainerProps> = ({ messages }) => {
  const messageWithTransitions = useTransition(
    messages,
    message => message.id,
    {
      from: { right: '-120%' },
      enter: { right: '0%' },
      leave: { right: '-120%' },
    },
  );
  return (
    <Container>
      {messageWithTransitions.map(({ item, key, props }) => (
        <Toast key={key} message={item} />
      ))}
    </Container>
  );
};

export default ToastContainer;

错误:

没有重载匹配这个调用。 重载 1 of 3, '(data: OneOrMore, props: () => { ref?: SpringRef | undefined; from?: TransitionFrom; ... 23 more ...; onDestroyed?: ((item: ToastMessage , key: Key) => void) | undefined; } | (object & {}), deps?: any[] | undefined): [...]',给出以下错误。 '(message: any) => any' 类型的参数不能分配给 '() => { ref?: SpringRef | 类型的参数不明确的;来自?:TransitionFrom; loop?: LoopProp> |不明确的; ... 22 更多 ...; onDestroyed?: ((item: ToastMessage, key: Key) => void) |不明确的; } | (目的 & {})'。 Overload 2 of 3, '(data: OneOrMore, props: { ref?: SpringRef | undefined; from?: TransitionFrom; ... 23 more ...; onDestroyed?: ((item: ToastMessage, key: Key ) => void) | undefined; } | (object & {}), deps: any[] | undefined): [...]',给出以下错误。 '{ 类型的参数 from: { right: string; };输入:{右:字符串; };离开:{ 对:字符串; }; }' 不能分配给“any[]”类型的参数。 对象字面量只能指定已知的属性,而 'from' 不存在于类型 'any[]' 中。 TS2769

 const ToastContainer: React.FC<ToastContainerProps> = ({ messages }) => {
const messageWithTransitions = useTransition(
                                   ^
     messages,
    message => message.id,

【问题讨论】:

  • 我认为您在版本 9 中使用了旧的 v8 语法。在版本 9 中,useTransition 发生了变化。您不再为其提供密钥,并且渲染部分正在使用渲染道具而不是地图。
  • Eu usei a versão V8 é funcionou, obrigado!

标签: reactjs typescript styled-components react-spring


【解决方案1】:

我也遇到了同样的问题,因为我也在参加训练营。 我不知道你是否已经解决了这个问题,但我会在此处粘贴解决方案:

import React from 'react';
import { useTransition } from 'react-spring';

import Toast from './Toast';

import { ToastMessage } from '../../hooks/toast';
import { Container } from './styles';

interface ToastContainerProps {
  messages: ToastMessage[];
}

const ToastContainer: React.FC<ToastContainerProps> = ({ messages }) => {
  const messagesWithTransitions = useTransition(messages, {
    keys: (message) => message.id,
    from: { right: '-120%' },
    enter: { right: '0%' },
    leave: { right: '-120%' },
  });

  return (
    <Container>
      {messagesWithTransitions((style, item, t) => (
        <Toast key={t.key} style={style} message={item} />
      ))}
    </Container>
  );
};

export default ToastContainer;

你可以在这里阅读useTransition钩子的变化,如果你想的话:Breaking Changes

【讨论】:

  • Obrigado para resolver rapidamente eu usingi a versão V8 que não tem essas modificações mas agora vou atualizar para utilizar a nova versão e vou usar sua resposta como base, obrigado pela ajuda amigo!
  • 不错!您能否将我的答案标记为解决方案,以防其他人寻找它。 PS.:我认为在堆栈溢出中你不能说英语以外的其他语言。
猜你喜欢
  • 2019-07-17
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 2021-08-30
  • 2020-12-27
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多