question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RadioButton.ref not working?

See original GitHub issue

Environment

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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
satya164commented, Oct 8, 2019

@DianaKoenraadt it should be status. My mistake. Updated the comment.

0reactions
DianaKoenraadtcommented, Oct 8, 2019

Found it!! Ended up with this:

function MyRadioButton({ onPress, text, ...rest }) {
    return (
        <Touchable onPress={onPress}>
            <View style={Styles.radioButton}>
                <RadioButton {...rest} />
                <Text>{text}</Text>
            </View>
        </Touchable>
    );
}

<RadioButton.Group
    onValueChange={value => this._setState(value)}
    value={this._getState()} >
        <MyRadioButton value="a" text="a" onPress={() => this._setState({ something: "a"})} />
        <MyRadioButton value="b" text="b" onPress={() => this._setState({ something: "b"})} />
</RadioButton.Group>

Thanks!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found