RadioButton.ref not working?
See original GitHub issueEnvironment
Android
Description
I cannot obtain the ref. I wanted to work around issue #1371 for now, by wrapping the RadioButton in a Touchable (Can be a TouchableOpacity too, doesn’t matter). But I cannot obtain the ref to do so.
Reproducible Demo
import React from 'react'; // eslint-disable-next-line no-unused-vars
import { View } from 'react-native';
import { RadioButton, Text } from 'react-native-paper';
import { Styles } from '../constants/Styles';
import Touchable from 'react-native-platform-touchable';
export default class MyRadioButton extends React.Component {
constructor(props) {
super(props);
this._radio = null;
}
_onPress() {
console.log('click!');
if (this._radio) {
console.log('radio');
//this._radio.click();
this._radio.handlePress();
//this._radio.onValueChange(true);
}
}
render() {
return (
<Touchable onPress={this._onPress}>
<View style={Styles.radioButton}>
<RadioButton value={this.props.value} ref={(ref) => this._radio = ref} />
<Text>{this.props.text}</Text>
</View>
</Touchable>
)
}
}
Above, this._radio
is and stays null. I also tried it with React.createRef()
but that doesn’t work either:
import React from 'react'; // eslint-disable-next-line no-unused-vars
import { View } from 'react-native';
import { RadioButton, Text } from 'react-native-paper';
import { Styles } from '../constants/Styles';
import Touchable from 'react-native-platform-touchable';
export default class MyRadioButton extends React.Component {
constructor(props) {
super(props);
this._radio = React.createRef();
}
_onPress() {
console.log('click!');
if (this._radio && this._radio.current) {
console.log('radio');
//this._radio.click();
this._radio.current.handlePress();
//this._radio.onValueChange(true);
}
}
render() {
return (
<Touchable onPress={this._onPress}>
<View style={Styles.radioButton}>
<RadioButton value={this.props.value} ref={this._radio} />
<Text>{this.props.text}</Text>
</View>
</Touchable>
)
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Radio ref on react - Stack Overflow
Refs provide a way to access DOM nodes or React elements created in the render method. You should create 2 different refs for...
Read more >How to Use Radio Buttons in ReactJS | Pluralsight
Because that name property puts all the radio buttons into a group, you can get the value once the user selects any of...
Read more >Radio Buttons In React.js
Example of how to use radio buttons in React.js. ... But it works differently, because in this case <input> is a React component,...
Read more >How to Set the disabled state in React RadioButton component
RadioButton component can be enabled/disabled by giving disabled property. To disable RadioButton component, the disabled property can be set as true .
Read more >return value of selected radio button with v-for - Laracasts
I tried to figure out how to delay it or call the function after click with no luck, then was reminded of refs....
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
@DianaKoenraadt it should be
status
. My mistake. Updated the comment.Found it!! Ended up with this:
Thanks!