Comments (6)
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
from react-native.
Newer Version of React Native is Available! | |
---|---|
ℹ️ | 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. |
from react-native.
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?
from react-native.
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>
)
}
from react-native.
SOLUTION FOR MY ISSUE
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 web
I 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.
PREVIOUS COMMENTS
react-native 0.73.4
I'm experiencing this issue.
- I wrapped the component in a forwardRef, but seems that it is not possible to expose any of the methods.
- I tried to useImperativeHandle to expose those methods, but it's not viable.
- The only solution I found is this one. Using an innerRef.
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.
#16247 (comment)
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.
from react-native.
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.
from react-native.
Related Issues (20)
- Android Crash: SIGSEGV 0x0000000000000000 - libc.so | Crashed: null pointer dereference HOT 5
- Google Api Error: Invalid request - Your app declares support for 'riscv64' architecture, but the following libraries are missing HOT 5
- ReactNative 0.68.0 -> 0.73.0 : much more slower on android release builds HOT 7
- ReactContext is getting null value in Android native project. HOT 3
- Extra line wrap on some Android devices on some combinations of lineHeight, letterSpacing and other related properties
- Just a test bug for React Native Dev Tools HOT 2
- [0.75.3] Build failing with: "RNGP - Autolinking: Could not find project.android.packageName in react-native config output!" HOT 13
- reactnative.dev SITE IS DOWN. IT HAS BEEN DOWN FOR MORE THAN 3 HOURS HOT 6
- AlignItems may sometimes become invalid on Android HOT 4
- I can't open the new architecture according to the steps on the official website, on ios and android platforms HOT 6
- Another DevTools Test Issue HOT 2
- Test issue HOT 2
- In IOS TextInput does not update style if copy and pasted string is bigger then maxLength HOT 8
- [id: 'com.facebook.react.settings'] > java.io.UncheckedIOException: HOT 3
- Run-android command is stuck without giving any output HOT 4
- Build failed on android with "Task : android:buildCMakeDebug[arm64-v8a] FAILED" HOT 8
- [Android] Headless Crash `Tried to finish non-existent task with id` HOT 3
- "Cannot find EventEmitter for receivedTouches" error in Android Native UI Component HOT 1
- Swift Package Manager (SPM) does not work with RN 0.75.2 HOT 5
- Error "no implicit conversion of String into Array" in Pod Install after migrating to RN > 0.74 (0.74.0 until 0.75.3) HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native.