Closed storyicon closed 1 month ago
:warning: | Newer Version of React Native is Available! |
---|---|
:information_source: | You are on a supported minor version, but it looks like there's a newer patch available - 0.74.2. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases. |
ERROR Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
The error message is pretty self-explanatory. What is not clear about it?
I don't believe this error should exist. The TextInput component itself provides the ref property. I've encountered this warning in many mature libraries, which makes me doubt its necessity. Do you have any suggestions for eliminating this warning in the example I provided? @cortinico
It should be written like this
import { TextInput } from 'react-native'
import { useRef } from 'react'
import { View } from 'react-native'
export default function Component() {
const textRef = useRef<TextInput>(null)
return (
<View>
<TextInput ref={textRef} />
</View>
)
}
In my case, the app was multiplatform (web, desktop, mobile) with files named:
/MyButton/index.tsx
/MyButton/MyButton/index.native.tsx
for mobile/MyButton/MyButton/index.tsx
for webI did not notice that there was a file at the root MyButton/index.tsx
which would render MyButton/MyButton/index.tsx
or MyButton/MyButton/index.native.tsx
.
I did not add the ref to the component MyButton/index.tsx
so for this reason the ref was not forwarded. Somehow having the same name of the file for web, I got sidetracked and thought was the same file.
react-native 0.73.4
I'm experiencing this issue.
I follow the react native docs on direct-manipulation. My implementation is exactly as in the example Composite components and setNativeProps
, but the ref is not forwarded (or the API is set private) and I don't have access to the native view method setNativeProps
.
In my use case, I need ref to a native View to call setNativeProps.
There must be some check to detect if the component is stateless, maybe this check is failing. Because I wrapped the component in a forwardRef as by the instructions.
https://github.com/facebook/react-native/issues/16247#issuecomment-341327893
for example, code similar to this will not work in my application. I did not try to reproduce with a minimum repro, but hopefully I'll get the opportunity to do that.
MyButton.js
function MyButton(props, ref) {
return (
<View ref={ref} />
);
}
export default forwardRef(MyButton);
MyContainer.js
function MyContainer(props) {
const myRef = useRef();
const hide = () => {
if (myRef.current) {
myRef.current.setNativeProps({opacity: 0});
}
}
return (
<TouchableOpacity onPress={() => hide()}>
<MyButton ref={myRef} />
</TouchableOpacity>
)
}
while this works:
MyButton.js
function MyButton(props, ref) {
return (
<View ref={props.myButtonRef} />
);
}
export default forwardRef(MyButton);
MyContainer.js
function MyContainer(props) {
const myRef = useRef();
const hide = () => {
if (myRef.current) {
myRef.current.setNativeProps({opacity: 0});
}
}
return (
<TouchableOpacity onPress={() => hide()}>
<MyButton myButtonRef={myRef} />
</TouchableOpacity>
)
}
I did not have the time to reproduce it outside of my application with a Minimum Reproducible Example. This is just and example, but I believe the ref is getting removed.
I receive the warning functional components cannot be given refs
, but the react-native documentation instruct us to use forward ref with functional components, to get a hold of the native view, so I believe here react-native is removing the ref, in fact if I set the ref without using the ref keyword on this functional/forwardRef component, it works.
I believe there is a check in the code, which removes the ref, but some of the conditions for removing this ref are failing.
The conditions maybe could be, if the component is a forwardRef, even if functional component, don't remove the ref, otherwise the ref is not forwarded.
UPDATE
I used useImperativeHandle to expose each method of the ref, but still does not work using the ref keyword from the functional component, but it does work if I pass the ref as innerRef prop instead of ref prop.
I believe I've figured out what caused the issue. It was related to NativeWind and the package https://www.npmjs.com/package/react-native-css-interop. After uninstalling it, the problem no longer occurs.
Description
When I use
ref
onTextInput
, this problem always occurs stably:Steps to reproduce
React Native Version
0.74.1
Affected Platforms
Runtime - Android
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://snack.expo.dev/@storyicon/honest-yellow-pretzel
Screenshots and Videos