Create Radio Button Component in React Native
Create Radio Button Component in React Native
N
PLA
This tutorial shows you how you can create a radio button component in React
Native application pretty smoothly. We will learn the easiest way to deal with
React Native Radio Buttons.
https://www.positronx.io/create-radio-button-component-in-react-native/ 1/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
source: inspiration.design
We will display four radio buttons, each containing name value, on user-
selected any value among the options will be printed on the frontend.
Scheda
Madre
Gigabyte
Z790 UD
LGA 170…
BPM-POWER
Ad
https://www.positronx.io/create-radio-button-component-in-react-native/ 2/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
Let’s implement Radio Buttons to React Native app employing a simple way.
react-native upgrade
https://www.positronx.io/create-radio-button-component-in-react-native/ 3/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
Martello
perforatore
Hikoki
DH26PC2
[DH26PC2]
BPM-POWER
Ad
cd rnradiobutton
https://www.positronx.io/create-radio-button-component-in-react-native/ 5/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
We will create the Radio Button component in other template and imported in
the App component.
Next, create a components folder at the root of your project and create
RadioButton.js file in it.
Define the RadioButton component and set the initial state of radio buttons.
https://www.positronx.io/create-radio-button-component-in-react-native/ 6/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
};
}
Inside the render() method define the radio button properties and state.
The map() method iterate over the radio button properties and extract the
value of currently selected radio button using onPress() event..
render() {
const { PROP } = this.props;
const { value } = this.state;
return (
<View>
{PROP.map(res => {
return (
<View key={res.key} style={styles.container}>
<Text style={styles.radioText}>{res.text}</Text>
<TouchableOpacity
style={styles.radioCircle}
onPress={() => {
this.setState({
value: res.key,
});
}}>
{value === res.key && <View style={styles.selected
</TouchableOpacity>
</View>
);
})}
<Text> Selected: {this.state.value} </Text>
</View>
);
}
To style the radio button we are using the StyleSheet component along with
CSS classes.
https://www.positronx.io/create-radio-button-component-in-react-native/ 7/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'space-between',
},
radioText: {
marginRight: 35,
fontSize: 20,
color: '#000',
fontWeight: '700'
},
radioCircle: {
height: 30,
width: 30,
borderRadius: 100,
borderWidth: 2,
borderColor: '#3740ff',
alignItems: 'center',
justifyContent: 'center',
},
selectedRb: {
width: 15,
height: 15,
borderRadius: 50,
backgroundColor: '#3740ff',
},
result: {
marginTop: 20,
color: 'white',
fontWeight: '600',
backgroundColor: '#F3FBFE',
},
});
Here is the final RadioButton.js file that we have already imported in App.js to
display the Radio Buttons.
https://www.positronx.io/create-radio-button-component-in-react-native/ 8/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
https://www.positronx.io/create-radio-button-component-in-react-native/ 9/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
selectedRb: {
width: 15,
height: 15,
borderRadius: 50,
backgroundColor: '#3740ff',
},
result: {
marginTop: 20,
color: 'white',
fontWeight: '600',
backgroundColor: '#F3FBFE',
},
});
react-native run-android
react-native run-ios
https://www.positronx.io/create-radio-button-component-in-react-native/ 10/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
Digamber
A Full-stack developer with a passion to solve real world
problems through functional programming.
Twitter GitHub
Recommended Posts:
https://www.positronx.io/create-radio-button-component-in-react-native/ 11/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
React Native Build & Validate Forms with Formik & Yup
https://www.positronx.io/create-radio-button-component-in-react-native/ 12/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
Email Address *
Subscribe
Explore
About
Affiliate Disclosure
Privacy Policy
Disclaimer
Contact
Categories
Angular
Vue
React
Ionic
https://www.positronx.io/create-radio-button-component-in-react-native/ 13/14
6/27/23, 2:23 PM Create Radio Button Component in React Native
Laravel
Codeigniter
PHP
https://www.positronx.io/create-radio-button-component-in-react-native/ 14/14