- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我查看了许多文档和示例,但我似乎仍然不太了解如何使用 forwardRef
在 React Native 中使用带有 TypeScript 的功能组件。下面是我创建 MyCustomComponent
的示例使用我尝试通过创建 ref 从父级调用的自定义函数。但是,由于 ref 定义不正确并且 null
,我显然收到一条错误消息,告诉我该函数不存在。请帮助我了解如何正确使用forwardRef
在 React Native 中。提前致谢!
interface MyCustomComponentProps {
title: string
}
const MyCustomComponent: React.FunctionComponent<MyCustomComponentProps> = React.forwardRef((props, ref) => {
const coolAlert = () => {
Alert.alert('Hey!', 'This was called from MyCustomComponent')
}
return (
<View>
<Text>{props.title}</Text>
</View>
)
})
export default function App () {
const MyCustomComponentRef = useRef()
return (
<SafeAreaView>
<MyCustomComponent ref={MyCustomComponentRef} title='Hello World' />
<TouchableOpacity
onPress={() => {
MyCustomComponentRef.coolAlert()
}}>
<Text>Click Me</Text>
</TouchableOpacity>
</SafeAreaView>
)
}
最佳答案
转发引用
Refs 可能真的很令人困惑,因为有多种方法可以处理它们,而且人们不知道 ref 对象( React.MutableRefObject
或 React.RefObject
)和存储在 .current
上的 ref 值之间的区别。 ref 对象的属性。您在这里犯了这个错误,以及一些缺失或不正确的 typescript 类型。useRef<T>
是一个通用钩子(Hook),其中值 T
告诉将存储什么类型的值。我们需要告诉 App
我们打算用 coolAlert
存储一些东西方法。实际上我们稍后会看到我们需要我们的 ref 是不可变的,所以我们将使用 createRef<T>
反而。
interface MyRef {
coolAlert(): void;
}
const MyCustomComponentRef = createRef<MyRef>();
当我们调用
onPress
,我们需要访问 ref 对象的当前值。通过将泛型添加到
createRef
, typescript 已经知道这个值是
MyRef
或
undefined
.我们可以调用
coolAlert
带有可选链接
?.
运算符(operator)。
onPress={() => MyCustomComponentRef.current?.coolAlert()}
现在我们需要在
MyCustomComponent
上做一些工作。 .你错误地给它分配了类型
React.FunctionComponent<MyCustomComponentProps>
因为函数组件不具备我们需要的 ref 转发知识。
function forwardRef<T, P = {}>(Component: RefForwardingComponent<T, P>): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
MyCustomComponent
的类型应该是来自
forwardRef
的复杂返回类型.但是我们不需要自己分配那个类型,我们只需要传递泛型
T
和
P
到
forwardRef
函数调用。
T
是 ref 和
P
的类型是 Prop 的类型。
const MyCustomComponent = React.forwardRef<MyRef, MyCustomComponentProps>(...
好的,所以我们
got rid of all the typescript errors !耶!除了……等一下。它实际上并没有做任何事情。所有这些,它仍然不起作用。
我讨厌裁判。裁判很糟糕。
MyCustomComponent
, 现在可以访问转发的 ref 并可以将其附加到 DOM 组件。但是我们不希望它附加到 DOM 元素上,我们希望它附加到
MyCustomComponent
.但我们真的做不到。
By default, you may not use the ref attribute on function components because they don’t have instances [docs]
useImperativeHandle
的钩子(Hook)。这感觉就像一个黑客解决方案,甚至文档都说“不要这样做”。是的,我讨厌裁判。
useImperativeHandle customizes the instance value that is exposed to parent components when using ref. As always, imperative code using refs should be avoided in most cases. useImperativeHandle should be used with forwardRef. [docs]
coolAlert
方法通过
useImperativeHandle
.
useImperativeHandle(ref , () => ({coolAlert}));
现在
it actually works , 最后!
关于javascript - 如何使用 TypeScript 在 React Native 中使用 forwardRef 和 FunctionComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64488719/
我创建了一个 FunctionComponent ,它应该有条件地返回它所涉及的子项。 Tab title
我正在使用 Typescript 和 ReactJS,我是个菜鸟。我正在尝试做一些我认为应该很简单的事情,但是 Typescript 阻碍了我。 我有两个 React 页面组件定义如下: const
我有一个像这样的 HOC: utils/withGlobalSettings.tsx interface Props { globalSettings: { ... }; } expo
我将如何在 typescript 中处理这个问题。我知道我可以通过给它起个名字来做到这一点,但这不是我要问的。我在问我怎么还能只使用默认值。 interface IProps { name: st
我从 React 和 Typescript 开始,我的代码编辑器声称我的 Button 组件(错误如下),但对于 eslint 似乎没问题。 import React from 'react' int
我从 React 和 Typescript 开始,我的代码编辑器声称我的 Button 组件(错误如下),但对于 eslint 似乎没问题。 import React from 'react' int
这两个例子完成了同样的事情。但是在引擎盖下有什么区别?我了解函数式组件与 React.Component 和 React.PureComponent 的对比,但我无法找到有关 React.Functi
我正在尝试为 React.FunctionComponent 添加类型定义到 .d.ts 文件,如下所示: Tree.propTypes = { root: PropTypes.object.is
如何在不触发使用 useState 的错误的情况下在 react 中创建一个被“监视”的输入元素数组在 FunctionComponent 的主体之外? 如果我有以下内容(未经测试的简化示例): in
React.FunctionComponent 和 React.SFC 有什么区别。我是 typescript 的新手,实际上我不知道什么时候该用一个。例如,当使用 React Hooks 时,我应该
我想创建一个 TypeScript 类型来保证 React FunctionComponent最终只渲染一个 HTML 元素。也就是说,它不能是 string , number , ReactFrag
我有以下 React 功能组件: import React, { memo } from 'react'; interface Props { buttonType?: JSX.Intrinsic
我收到此错误 client:159 [at-loader] ./src/App.tsx:40:46 TS2322: Type 'unknown' is not assignable to type '
我收到此错误 client:159 [at-loader] ./src/App.tsx:40:46 TS2322: Type 'unknown' is not assignable to type '
我正在尝试从 SVG 导入 React functionComponent,然后将其作为 Prop 发送到另一个组件以呈现该 svg。使用下面的设置,这可以很好地编译,但最终在尝试在浏览器中呈现 sv
这工作正常: import React, { FunctionComponent } from "react"; const Counter: FunctionComponent = () => he
我查看了许多文档和示例,但我似乎仍然不太了解如何使用 forwardRef在 React Native 中使用带有 TypeScript 的功能组件。下面是我创建 MyCustomComponent
我正在尝试在 React 应用程序中添加 TypeScript。 版本: "react": "16.9.0", "typescript": "3.5.3", 我有一个像这样的数组 import aLo
我正在学习 Typescript-react,但我陷入了这个错误 Type '({ items }: PropsWithChildren) => Element[]' is not assignabl
我是一名优秀的程序员,十分优秀!