小编Uma*_*ruq的帖子

navigation.navigate('Home') 在打字稿中显示一些错误

当我使用 useNavigation 或 props { navigation } 使用 navigation.navigate('Home') 在屏幕之间导航时,打字稿返回错误 Argument of type '"Main"' is not allocate to type '{ key: string; 参数?:未定义;合并?:布尔值 | 不明确的; } | { 名称:从不;键?: 字符串 | 不明确的; 参数:从不;合并?:布尔值 | 不明确的; }' 这是什么意思?

下面是我的代码:

import React from 'react';
import { View } from 'react-native';
import { Button } from 'react-native-paper';
import { useNavigation } from '@react-navigation/native';

const Home: React.FC = () => {
  const navigation = useNavigation();

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: …
Run Code Online (Sandbox Code Playgroud)

react-native react-navigation

31
推荐指数
4
解决办法
4万
查看次数

用打字稿反应forwardRef

我已经使用 React 转发了 ref,forwardRef下面是我的代码:

interface PropsDummy {}

const ProfileMenu = forwardRef<HTMLInputElement, PropsDummy>((props, ref) => {
  console.log(ref.current);
}
Run Code Online (Sandbox Code Playgroud)

但为什么这会导致打字稿错误?

Property 'current' does not exist on type '((instance: HTMLInputElement | null) => void) | MutableRefObject<HTMLInputElement | null>'
Run Code Online (Sandbox Code Playgroud)

但如果我使用别名,当前对象可以很好地工作而不会出现打字稿错误

interface PropsDummy {}

const ProfileMenu = forwardRef<HTMLInputElement, PropsDummy>((props, ref) => {
  const myRef = ref as React.RefObject<HTMLInputElement>;
  console.log(myRef.current);
}
Run Code Online (Sandbox Code Playgroud)

如何在没有打字稿错误的情况下获取当前对象?

谢谢

typescript reactjs next.js

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