-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef() #278
Comments
get the same error when using expo-router with react-native-paper. Here is the example: import { Link } from 'expo-router';
import { Button, Text } from 'react-native-paper';
export default function Comp() {
return (
<Link href="/" asChild>
<Button mode="contained-tonal">
<Text>Semua</Text>
</Button>
</Link>
/>
);
} |
I think the child component needs forwardRef:
|
Did you ever find a fix? |
I think "react-native-paper" Button cant not receive ref |
You use custom component without React.forwardRef in first example. and Text component maybe built in Class component or use React.forwardRef. so, you must wrap your custom component to"forwardRef" , if you want to pass ref |
I also had this problem. My adapted component looks like this: import React from "react";
import {
GestureResponderEvent,
Pressable,
StyleSheet,
Text,
View,
useColorScheme,
} from "react-native";
import generalStyles from "../../styles/general";
import { COLORS, Theme } from "../../styles/theme.style";
interface IButton {
onPress: (event: GestureResponderEvent) => void;
onClick?: (event: GestureResponderEvent) => void;
title: string;
style?: Record<string, unknown>;
textStyle?: Record<string, unknown>;
}
const CustomButton = React.forwardRef<View, IButton>((props, ref) => {
const { title = "Enter", style = {}, textStyle = {}, onPress } = props;
const theme = Theme[useColorScheme() === "dark" ? "dark" : "light"];
return (
<Pressable
ref={ref}
onPress={onPress}
style={({ pressed }) => [
styles.button,
pressed ? theme.button.pressed : theme.button,
generalStyles.button,
theme.button,
style,
]}
>
<Text
style={[
{ color: COLORS.white },
generalStyles.text,
styles.text,
textStyle,
]}
>
{title}
</Text>
</Pressable>
);
});
CustomButton.displayName = "CustomButton";
export default CustomButton;
const styles = StyleSheet.create({
button: {
display: "flex",
borderRadius: 5,
justifyContent: "center",
alignItems: "center",
},
text: {
padding: 3,
},
}); |
FYI, for anyone running into this issue using the core React Native Button component, they merged a fix a few months back that was recently made available in |
I have a similar issue here. It only happens with |
same problem for me
|
I just ran into this as well whenever I use a |
Summary
We receive an Yellow screen warning with the error mentioned above with example 1 whereas it works fine for example 2.
Minimal reproducible example
Example 1
Example 2
The text was updated successfully, but these errors were encountered: