{"id":125,"date":"2025-09-26T14:56:22","date_gmt":"2025-09-26T14:56:22","guid":{"rendered":"https:\/\/blog.vyomscode.com\/?p=125"},"modified":"2025-09-26T14:56:22","modified_gmt":"2025-09-26T14:56:22","slug":"adding-a-picker-in-react-native","status":"publish","type":"post","link":"https:\/\/blog.vyomscode.com\/index.php\/2025\/09\/26\/adding-a-picker-in-react-native\/","title":{"rendered":"Adding a Picker in React Native"},"content":{"rendered":"\n<p>When building forms in React Native, dropdowns are often needed to let users choose between a set of options. The <strong><code>@react-native-picker\/picker<\/code><\/strong> package is a widely used solution that integrates smoothly with React Native apps.<\/p>\n\n\n\n<p>In this blog, we\u2019ll cover two approaches:<\/p>\n\n\n\n<ol>\n<li>A simple picker.<\/li>\n\n\n\n<li>A picker that dynamically shows additional fields based on the selected value.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1 \u2013 Install the Picker Package<\/h4>\n\n\n\n<p>Run the following command in your project terminal:<\/p>\n\n\n\n<p><strong><em>npm install @react-native-picker\/picker<\/em><\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2 \u2013 Simple Picker Example<\/h4>\n\n\n\n<p>Here\u2019s a basic picker that allows users to answer <strong>\u201cDo you own any car?\u201d<\/strong> with \u201cYes\u201d or \u201cNo\u201d.<\/p>\n\n\n\n<p><strong><em>import { useState } from &#8216;react&#8217;;<br>import { StyleSheet, Text, View } from &#8216;react-native&#8217;;<br>import { Picker } from &#8216;@react-native-picker\/picker&#8217;;<br><br>const PickerExample = () => {<br>const [ownAnyCar, setOwnAnyCar] = useState(&#8216;No&#8217;);const handleOwnAnyCarChange = (itemValue: string) => {<br>setOwnAnyCar(itemValue);<br>};<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>return (\n&lt;>\n      &lt;Text style={{ marginLeft: 3 }}>Do You Own Any Car&lt;\/Text>\n      &lt;View style={styles.pickerContainer}>\n        &lt;Picker\n          style={styles.picker}\n          selectedValue={ownAnyCar}\n          onValueChange={handleOwnAnyCarChange}\n        >\n          &lt;Picker.Item label=\"No\" value=\"No\" \/>\n          &lt;Picker.Item label=\"Yes\" value=\"Yes\" \/>\n        &lt;\/Picker>\n      &lt;\/View>\n    &lt;\/>\n);\n};const styles = StyleSheet.create({\npickerContainer: {\nbackgroundColor: 'white',\nheight: 40,\nborderColor: 'lightgrey',\nborderWidth: 1,\nmarginBottom: 10,\nwidth: '100%',\nborderRadius: 10,\nmarginTop: 2,\n},\npicker: {\nmarginTop: -6,\nwidth: '100%',\nfontSize: 15,\n},\n});\nexport default PickerExample;<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 This will render a dropdown where the user can select <strong>Yes<\/strong> or <strong>No<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3 \u2013 Picker with Conditional Fields<\/h4>\n\n\n\n<p>Now let\u2019s make the picker smarter. If the user selects <strong>\u201cYes\u201d<\/strong>, additional input fields will appear asking for the <strong>quantity of cars<\/strong> and their <strong>details<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong><em>import { useState } from 'react';\r\nimport { StyleSheet, Text, TextInput, View } from 'react-native';\r\nimport { Picker } from '@react-native-picker\/picker';\r\n\r\nconst PickerExtra = () => {\r\n  const &#91;ownAnyCar, setOwnAnyCar] = useState('No');\r\n  const &#91;showCarFields, setShowCarFields] = useState(false);\r\n  const &#91;quantityOfCars, setQuantityOfCars] = useState('');\r\n  const &#91;carDetails, setCarDetails] = useState('');\r\n\r\n  const handleOwnAnyCarChange = (itemValue: string) => {\r\n    setOwnAnyCar(itemValue);\r\n    setShowCarFields(itemValue === 'Yes');\r\n  };\r\n\r\n  return (\r\n    &lt;>\r\n      &lt;Text style={{ marginLeft: 3 }}>Do You Own Any Car&lt;\/Text>\r\n      &lt;View style={styles.pickerContainer}>\r\n        &lt;Picker\r\n          style={styles.picker}\r\n          selectedValue={ownAnyCar}\r\n          onValueChange={handleOwnAnyCarChange}\r\n        >\r\n          &lt;Picker.Item label=\"No\" value=\"No\" \/>\r\n          &lt;Picker.Item label=\"Yes\" value=\"Yes\" \/>\r\n        &lt;\/Picker>\r\n      &lt;\/View>\r\n\r\n      {showCarFields &amp;&amp; (\r\n        &lt;>\r\n          &lt;TextInput\r\n            style={styles.input}\r\n            onChangeText={setQuantityOfCars}\r\n            value={quantityOfCars}\r\n            placeholder=\"Quantity Of Cars\"\r\n            placeholderTextColor={'darkgrey'}\r\n          \/>\r\n\r\n          &lt;TextInput\r\n            style={styles.input}\r\n            onChangeText={setCarDetails}\r\n            value={carDetails}\r\n            placeholder=\"Each Car Details\"\r\n            placeholderTextColor={'darkgrey'}\r\n          \/>\r\n        &lt;\/>\r\n      )}\r\n    &lt;\/>\r\n  );\r\n};\r\n\r\nconst styles = StyleSheet.create({ \r\n  pickerContainer: {\r\n    backgroundColor: 'white',\r\n    height: 40,\r\n    borderColor: 'lightgrey',\r\n    borderWidth: 1,\r\n    marginBottom: 10,\r\n    width: '100%',\r\n    borderRadius: 10,\r\n    marginTop: 2,\r\n  },\r\n  picker: {\r\n    marginTop: -6,\r\n    width: '100%',\r\n    fontSize: 15,\r\n  },  \r\n  input: {\r\n    height: 40,\r\n    borderColor: 'lightgrey',\r\n    borderWidth: 1,\r\n    marginBottom: 10,\r\n    paddingHorizontal: 10,\r\n    width: '100%',\r\n    borderRadius: 10,\r\n    marginTop: 2,\r\n    backgroundColor: 'white'\r\n  },\r\n});\r\n\r\nexport default PickerExtra;\r\n<\/em><\/strong><\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 Here\u2019s what happens:<\/p>\n\n\n\n<ul>\n<li>When the user selects <strong>No<\/strong>, only the dropdown is visible.<\/li>\n\n\n\n<li>When the user selects <strong>Yes<\/strong>, two additional text inputs appear.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>The <code>@react-native-picker\/picker<\/code> package makes it easy to implement dropdowns in React Native.<\/p>\n\n\n\n<ul>\n<li>Start with a <strong>simple picker<\/strong> for straightforward options.<\/li>\n\n\n\n<li>Enhance the experience by <strong>conditionally rendering fields<\/strong> when certain options are chosen.<\/li>\n<\/ul>\n\n\n\n<p>This approach is extremely useful in forms, especially when you need to gather <strong>dependent information<\/strong> from users.<\/p>\n\n\n\n<p><a href=\"https:\/\/tripti.vyomscode.com\/\" data-type=\"link\" data-id=\"https:\/\/tripti.vyomscode.com\/\">Tripti<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building forms in React Native, dropdowns are often needed to let users choose between a set of options. The @react-native-picker\/picker package is a widely used solution that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/posts\/125"}],"collection":[{"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":1,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/posts\/125\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vyomscode.com\/index.php\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}