Unable to declare ref for TextInput
See original GitHub issueEnvironment
react-native-paper 3.0.0-alpha.3 react-native 0.59.8 Typescript 3.6.2
Description
I’m trying to convert my component that uses TextInput to typescript, but I’m getting an error at React.RefObject<TextInput>
: ‘TextInput’ refers to a value, but is being used as a type here.ts(2749)
I tried yarn add https://github.com/callstack/react-native-paper.git
to try to pull in #1218, but the command failed with this error:
✖ Errors found when building definition files.
example/src/index.native.tsx(1,25): error TS2307: Cannot find module 'expo'.
example/src/index.native.tsx(2,30): error TS2307: Cannot find module 'expo-keep-awake'.
example/src/index.native.tsx(18,39): error TS2307: Cannot find module 'react-navigation'.
example/src/RootNavigator.native.tsx(2,38): error TS2307: Cannot find module 'react-navigation'.
bob build
build files for publishing
Options:
--help Show help [boolean]
--version Show version number [boolean]
Error: Failed to build definition files.
at build (/Users/frank/Library/Caches/Yarn/v4/.tmp/37678653438a710e932e2eb30c14dbbe.d644354d396ad8e765c85886ab3033b45f7a7c00.prepare/node_modules/@react-native-community/bob/lib/targets/typescript.js:63:11)
at <anonymous>
error Command failed with exit code 1.
Reproducible Demo
import React from 'react';
import { TextInput } from 'react-native-paper';
class MyScreen extends React.Component<any, any> {
private input: React.RefObject<TextInput> = React.createRef();
render() {
return <TextInput ref={this.input} ... />
}
onSubmit() {
this.input.current.focus()
}
}
Am I doing this right? I know the ref spec in general went through a few iterations. Great library by the way, beautiful components. 👍
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:7
Top Results From Across the Web
React Native TextInput ref always undefined - Stack Overflow
I have looked around but no one seems to be having my problem, usually they have a typo or didn't bind the function...
Read more ><input>: The Input (Form Input) element - HTML
The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide...
Read more >Uncontrolled Components - React
In React, an <input type="file" /> is always an uncontrolled component because its value can only be set by a user, and not...
Read more >Working with refs in React | CSS-Tricks
The ref is created in the constructor and then attached to the input element when it renders. When the button is clicked, the...
Read more >Everything You Need to Know About Refs in React
createRef (); return ( <div> <input ref={textInput} /> <button onClick={() => textInput.current.focus()}> Click to Focus </ ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Per the lines here: https://github.com/callstack/react-native-paper/blob/efe17beafcc3bd83f4d84971499bd85647df9c84/src/components/TextInput/TextInput.tsx#L21-L23
I think you just need to use the react-native TextInput type for your ref, rather than trying to use the react-native-paper version.
Based on @savagematt’s example, I’d try:
I haven’t tested the above though. I’m using function components as seen here: https://github.com/callstack/react-native-paper/issues/1453#issuecomment-699163546
Has react native removed the ref attribute for TextInput? It’s no longer in the docs