javascript - TypeError:组件不是添加 forwardRef 的函数
问题描述
嗨,我试图将 forwardRef 添加到子组件,但在添加 forwardRef 时得到以下信息:
TypeError: Component is not a function
组件定义如下:
import React from 'react';
import { forwardRef } from 'react';
import { TextInputFocusEventData } from 'react-native';
import { NativeSyntheticEvent, StyleSheet } from 'react-native';
import { Input, InputProps } from 'react-native-elements';
import { Metrics, Colors } from '../../theme';
import Icons from '../Icons';
const CustomTextInput2 = forwardRef<TextInput, ICustomtextnputProps>((props, ref) => {
const { name, required } = props;
return (
<Input
rightIcon={<Icons name="right" />}
placeholder={name?.concat(required ? '*' : '')}
inputContainerStyle={styles.inputContainer}
inputStyle={styles.inputText}
{...props}
/>
)
});
....
这是堆栈跟踪:
ExceptionsManager.js:180 TypeError: Component is not a function
This error is located at:
in Unknown (at AddDetailsModal/index.tsx:67)
in RCTView (at View.js:34)
in View (at AddDetailsModal/index.tsx:66)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:217)
in AnimatedComponent (at createAnimatedComponent.js:278)
in AnimatedComponentWrapper (at CustomModal/index.tsx:71)
in RCTView (at View.js:34)
in View (at createAnimatedComponent.js:217)
in AnimatedComponent (at createAnimatedComponent.js:278)
in AnimatedComponentWrapper (at TouchableOpacity.js:221)
in TouchableOpacity (at TouchableOpacity.js:271)
in Unknown (at CustomModal/index.tsx:70)
in RCTView (at View.js:34)
in View (at KeyboardAvoidingView.js:220)
in KeyboardAvoidingView (at CustomModal/index.tsx:69)
in RCTView (at View.js:34)
in View (at AppContainer.js:107)
in RCTView (at View.js:34)
in View (at AppContainer.js:134)
in AppContainer (at Modal.js:228)
in RCTView (at View.js:34)
in View (at Modal.js:249)
in VirtualizedListContextResetter (at Modal.js:247)
in RCTModalHostView (at Modal.js:234)
in Modal (at CustomModal/index.tsx:63)
有什么我必须更改才能向该组件添加 forwardRef 吗?
解决方案
通过 React.forwardRef 包装组件并从父组件传递 ref 后,您需要重新加载应用程序!我现在遇到了这个问题,看到了同样的错误信息,只需重新加载应用程序即可解决
推荐阅读
- python - SQL 错误 (1100): 表 'authors' 未使用 LOCK TABLES 查询锁定
- c# - 尝试在标签助手中使用注入的 IActionContextAccessor 时出现访问冲突
- kotlin - gradle nativeBinaries 无法满足依赖关系
- google-bigquery - 如何在数组上使用 percentile_disc
- hibernate - JPA AttributeOverride.column() 应该覆盖所有列
- java - MapView 地图未在运行时呈现 {Screenshot Attached}
- python - Python Selenium WebDriverException:消息:chrome 不可访问,当两者都是最新的
- ios - SF Symbol imageView 不符合pointSize?
- python - 当提供的数据是 XML 时,如何从检查菜单中检索相同的 HTML?
- c# - 为实现分组的 UWP XAML 创建自定义 ICollectionView 对象