{"id":42980,"date":"2016-12-16T14:58:19","date_gmt":"2016-12-16T09:28:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42980"},"modified":"2018-02-26T12:23:10","modified_gmt":"2018-02-26T06:53:10","slug":"how-to-solve-some-common-coding-challenges-in-react-native","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/how-to-solve-some-common-coding-challenges-in-react-native\/","title":{"rendered":"How to Solve Some Common Coding Challenges in React Native?"},"content":{"rendered":"<p><a title=\"React Native Development Company\" href=\"http:\/\/www.tothenew.com\/react-native-development-services\">React Native<\/a> is a\u00a0framework that provides the facility to develop applications for both <a title=\"Android Application Development Services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">Android <\/a>and iOS.\u00a0If you are beginner on React Native, you can read more about it on <a title=\"React vs Angular \" href=\"http:\/\/www.tothenew.com\/blog\/react-js-vs-angular-2\/\">React Native Concept<\/a> or <a title=\"React Native Website\" href=\"http:\/\/facebook.github.io\/react-native\/\" target=\"_blank\">React Native Website<\/a>.<\/p>\n<p>For the last couple of months, I have been extensively working on this framework and through this blog I wanted to share some of the challenges I faced while <a title=\"React Native Components\" href=\"http:\/\/www.tothenew.com\/blog\/write-once-use-anywhere-react-native-components\/\">coding with React Native<\/a>.<\/p>\n<p>The challenges are documented as follows;<\/p>\n<p>1) <strong>How we can add the style classes dynamically in render function?<\/strong><br \/>\nIn React Native we don\u2019t have <code>display : none<\/code> property. If there is no <code>display:none<\/code> property then the question arises how one can hide\/show a view or a component.<\/p>\n<p>2) <strong>How to make a list scrollable if we are using ListView component of React Native?<\/strong><\/p>\n<p>So, let&#8217;s discuss this one by one and understand how to solve them<\/p>\n<p><strong>We deliberately created certain solutions we thought will be helpful which are given below. <\/strong><\/p>\n<p>1) In React Native we don\u2019t have display : none property. But if needed one can hide the component using the style properties <code>height, width, opacity<\/code><\/p>\n<p>[js]<br \/>\n\/\/index.android.js file<\/p>\n<p>import React, { Component } from &#8216;react&#8217;;<br \/>\nimport {<br \/>\n    AppRegistry,<br \/>\n    StyleSheet,<br \/>\n    Text,<br \/>\n    View<br \/>\n} from &#8216;react-native&#8217;;<br \/>\nexport default class ReactNativeHacks extends Component {<br \/>\n  render() {<br \/>\n    return (<br \/>\n        &lt;View style={styles.container}&gt;<br \/>\n          &lt;Text style={styles.welcome}&gt;<br \/>\n            Welcome to React Native!<br \/>\n          &lt;\/Text&gt;<br \/>\n          &lt;Text style={styles.instructions}&gt;<br \/>\n            To get started, edit index.android.js<br \/>\n          &lt;\/Text&gt;<br \/>\n          &lt;Text style={styles.instruction2}&gt;<br \/>\n            Double tap R on your keyboard to reload,{&#8216;\\n&#8217;}<br \/>\n            Shake or press menu button for dev menu<br \/>\n          &lt;\/Text&gt;<br \/>\n        &lt;\/View&gt;<br \/>\n    );<br \/>\n  }<br \/>\n}<br \/>\nconst styles = StyleSheet.create({<br \/>\n  container: {<br \/>\n    flex: 1,<br \/>\n    justifyContent: &#8216;center&#8217;,<br \/>\n    alignItems: &#8216;center&#8217;,<br \/>\n    backgroundColor: &#8216;#F5FCFF&#8217;,<br \/>\n  },<br \/>\n  welcome: {<br \/>\n    fontSize: 20,<br \/>\n    textAlign: &#8216;center&#8217;,<br \/>\n    margin: 10,<br \/>\n  },<br \/>\n  instructions: {<br \/>\n    textAlign: &#8216;center&#8217;,<br \/>\n    color: &#8216;#333333&#8217;,<br \/>\n    marginBottom: 5,<br \/>\n   },<br \/>\n  instruction2: {<br \/>\n    textAlign: &#8216;center&#8217;,<br \/>\n    color: &#8216;#333333&#8217;,<br \/>\n    marginBottom: 5,<br \/>\n  }<br \/>\n});<br \/>\nAppRegistry.registerComponent(&#8216;ReactNativeHacks&#8217;, () =&gt; ReactNativeHacks);<br \/>\n[\/js]<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-42987  aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download1-150x150.png\" alt=\"download\" width=\"184\" height=\"184\" \/><\/p>\n<p>Now if we want to hide the second paragraph we can use the following class<\/p>\n<p>[js]<br \/>\ninstructions: {<br \/>\n  textAlign: &#8216;center&#8217;,<br \/>\n  color: &#8216;#333333&#8217;,<br \/>\n  marginBottom: 5,<br \/>\n  opacity:0<br \/>\n},                                                 <\/p>\n<p>[\/js]<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-42993 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-2-169x300.png\" alt=\"download (2)\" width=\"169\" height=\"300\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-2-169x300.png 169w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-2.png 255w\" sizes=\"(max-width: 169px) 100vw, 169px\" \/><\/p>\n<p>or can use the style class with <code>height, width<\/code><\/p>\n<p>[js]<br \/>\ninstructions: {<br \/>\n textAlign: &#8216;center&#8217;,<br \/>\n color: &#8216;#333333&#8217;,<br \/>\n marginBottom: 5,<br \/>\n height:0,<br \/>\n width:0,<br \/>\n opacity:0<br \/>\n},<br \/>\n[\/js]<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-42994 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-3-171x300.png\" alt=\"download (3)\" width=\"171\" height=\"300\" \/><\/p>\n<p>Using only opacity does not free the space occupied by the text or view component. So for that what we need to do is just add the height and width of component 0.<\/p>\n<p><strong>Use the below command to show \/ hide component or to add or remove styling classes dynamically.<\/strong><\/p>\n<p>[js]<br \/>\n\/\/index.android.js file<\/p>\n<p>import React, { Component } from &#8216;react&#8217;;<br \/>\nimport {<br \/>\n    AppRegistry,<br \/>\n    StyleSheet,<br \/>\n    Text,<br \/>\n    View,<br \/>\n    TouchableNativeFeedback<br \/>\n} from &#8216;react-native&#8217;;<br \/>\nexport default class ReactNativeHacks extends Component {<br \/>\n  constructor(props){<br \/>\n    super(props);<br \/>\n    this.state = {<br \/>\n      flag: true<br \/>\n    }<br \/>\n  }<br \/>\n  buttonClick() {<br \/>\n   this.setState({<br \/>\n     flag: !this.state.flag \/\/ toggle state `flag` on button click<br \/>\n   })<br \/>\n  }<br \/>\n  render() {<br \/>\n    return (<br \/>\n    &lt;View style={styles.container}&gt;<br \/>\n     { this.state.flag &amp;&amp;   \/\/ show\/hide on basis of `this.state.flag` value<br \/>\n            &lt;Text style={styles.welcome}&gt;<br \/>\n              Hide or Show text dynamically by just clicking the button.<br \/>\n            &lt;\/Text&gt;<br \/>\n     }<br \/>\n     &lt;TouchableNativeFeedback&gt;<br \/>\n      &lt;View style={styles.button}&gt;<br \/>\n       &lt;Text style={styles.buttonText} \/\/call function `buttonClick`<br \/>\n             onPress={() =&gt; { this.buttonClick()}}&gt;Click here.&lt;\/Text&gt;<br \/>\n      &lt;\/View&gt;<br \/>\n     &lt;\/TouchableNativeFeedback&gt;<br \/>\n     &lt;\/View&gt;<br \/>\n    );<br \/>\n  }<br \/>\n}<br \/>\nconst styles = StyleSheet.create({<br \/>\n  container: {<br \/>\n    flex: 1,<br \/>\n    justifyContent: &#8216;center&#8217;,<br \/>\n    alignItems: &#8216;center&#8217;,<br \/>\n    backgroundColor: &#8216;#F5FCFF&#8217;,<br \/>\n  },<br \/>\n  welcome: {<br \/>\n    fontSize: 20,<br \/>\n    textAlign: &#8216;center&#8217;,<br \/>\n    margin: 10,<br \/>\n  },<br \/>\n button: {<br \/>\n   backgroundColor: &#8216;#ff8000&#8217;,<br \/>\n   borderRadius: 4,<br \/>\n   padding:10<br \/>\n },<br \/>\n  buttonText : {<br \/>\n    color : &#8216;#fff&#8217;,<br \/>\n    textAlign:&#8217;center&#8217;,<br \/>\n    fontWeight: &#8216;bold&#8217;<br \/>\n  },<br \/>\n});<br \/>\nAppRegistry.registerComponent(&#8216;ReactNativeHacks&#8217;, () =&gt; ReactNativeHacks);<br \/>\n[\/js]<\/p>\n<p>In this example the state that defines whether a component should be displayed or not is decided by the state <code>flag<\/code>.<\/p>\n<p>[js]<br \/>\n { this.state.flag &amp;&amp;<br \/>\n      &lt;Text style={styles.welcome}&gt;<br \/>\n        Hide or Show text dynamically by just clicking the button.<br \/>\n       &lt;\/Text&gt;<br \/>\n }<br \/>\n[\/js]<\/p>\n<p>The same output can be generated using the code like :<br \/>\nBy adding this code, if <code>this.state.flag<\/code> is true then statement &#8216;Hide or Show text dynamically by just clicking the button.&#8217; displays otherwise the null is rendered.<\/p>\n<p>We can also provide other component in place of null that we want to render.<\/p>\n<p>[js]<br \/>\n{ this.state.flag ?<br \/>\n    &lt;Text style={styles.welcome}&gt;<br \/>\n      Hide or Show text dynamically by just clicking the button.<br \/>\n    &lt;\/Text&gt; : null<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>We can also show\/hide using the styling class.<br \/>\nThis also produces the same result as the above code. But in this we provide the styles dynamically according to the value of <code>this.state.flag<\/code>. If <code>this.state.flag<\/code> is true then <code>styles.welcome<\/code> class is applied otherwise <code>styles.hideClass<\/code> is applied which has <code>heigh: 0<\/code> <code>width: 0<\/code>.<\/p>\n<p>[js]<br \/>\n&lt;Text style={this.state.flag ? styles.welcome : styles.hideClass}&gt;<br \/>\n  Hide or Show text dynamically by just clicking the button.<br \/>\n&lt;\/Text&gt;<br \/>\n[\/js]<\/p>\n<p>Define hideClass in Stylesheet.create()<\/p>\n<p>[js]<br \/>\nhideClass: {<br \/>\n  height: 0,<br \/>\n  width: 0<br \/>\n}<br \/>\n[\/js]<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-42996 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-4-300x227.png\" alt=\"download (4)\" width=\"300\" height=\"227\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-4-300x227.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-4.png 601w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>In this output the text hides as soon as the button is clicked.<\/p>\n<p>2) In case of listview, the list is scrollable by default, but in few cases, the list is not scrollable, so if someone wants to scroll the list vertically just define the flex property as 1 to all the parent views of the listview.<\/p>\n<p>[js]<br \/>\n\/\/index.android.js file<\/p>\n<p>import React, { Component } from &#8216;react&#8217;;<br \/>\nimport {<br \/>\n    AppRegistry,<br \/>\n    StyleSheet,<br \/>\n    Text,<br \/>\n    View,<br \/>\n    ListView,<br \/>\n} from &#8216;react-native&#8217;;<br \/>\nexport default class ReactNativeHacks extends Component {<br \/>\n  constructor() {<br \/>\n    super();<br \/>\n    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) =&gt; r1 !== r2});<br \/>\n    this.state = {<br \/>\n      dataSource: ds.cloneWithRows([<br \/>\n        &#8216;row 1&#8217;, &#8216;row 2&#8217;, &#8216;row 3&#8217;, &#8216;row 4&#8217;, &#8216;row 5&#8217;, &#8216;row 6&#8217;, &#8216;row 7&#8217;, &#8216;row 8&#8217;, &#8216;row 9&#8217;, &#8216;row 10&#8217;, &#8216;row 11&#8217;, &#8216;row 12&#8217;, &#8216;row 13&#8217;<br \/>\n      ]),<br \/>\n    };<br \/>\n  }<br \/>\n  render() {<br \/>\n    return (<br \/>\n     &lt;View style={styles.container}&gt;<br \/>\n       &lt;Text style={styles.welcome}&gt;<br \/>\n        Example of Listview<br \/>\n       &lt;\/Text&gt;<br \/>\n       &lt;View style={styles.container2}&gt; \/\/flex:1, parent View of ListView<br \/>\n        &lt;Text style={styles.listData}&gt;Scrollable List&lt;\/Text&gt;<br \/>\n        &lt;ListView<br \/>\n              dataSource={this.state.dataSource}<br \/>\n              renderRow={(rowData) =&gt;<br \/>\n               &lt;Text style={styles.listValue}&gt;{rowData}&lt;\/Text&gt; }<br \/>\n        \/&gt;<br \/>\n       &lt;\/View&gt;<br \/>\n       &lt;Text style={styles.welcome}&gt;<br \/>\n            End of Listview<br \/>\n       &lt;\/Text&gt;<br \/>\n     &lt;\/View&gt;<br \/>\n    );<br \/>\n  }<br \/>\n}<br \/>\nconst styles = StyleSheet.create({<br \/>\n  container: {<br \/>\n    flex: 1,<br \/>\n    backgroundColor: &#8216;#F5FCFF&#8217;,<br \/>\n    padding: 20<br \/>\n  },<br \/>\n  container2: {<br \/>\n    flex: 1,<br \/>\n  },<br \/>\n  listData: {<br \/>\n    fontSize: 20,<br \/>\n  },<br \/>\n  listValue: {<br \/>\n    fontSize: 20,<br \/>\n    paddingVertical:10<br \/>\n  },<br \/>\n  welcome: {<br \/>\n    fontSize: 20,<br \/>\n    textAlign: &#8216;center&#8217;,<br \/>\n    margin: 10,<br \/>\n  },<br \/>\n});<br \/>\nAppRegistry.registerComponent(&#8216;ReactNativeHacks&#8217;, () =&gt; ReactNativeHacks);<br \/>\n[\/js]<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"size-medium wp-image-42997 aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-5-300x231.png\" alt=\"download (5)\" width=\"300\" height=\"231\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-5-300x231.png 300w, \/blog\/wp-ttn-blog\/uploads\/2016\/12\/download-5.png 599w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Find the code snippets\u00a0<a title=\"react-native-gotchas\" href=\"https:\/\/github.com\/priya1091992\/React-Native-Gotchas-1\" target=\"_blank\">here<\/a>\u00a0for the above examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React Native is a\u00a0framework that provides the facility to develop applications for both Android and iOS.\u00a0If you are beginner on React Native, you can read more about it on React Native Concept or React Native Website. For the last couple of months, I have been extensively working on this framework and through this blog I [&hellip;]<\/p>\n","protected":false},"author":1027,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[3429,1772,1],"tags":[4845,4255,4256,3566,55,3505,3512,2046],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42980"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1027"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42980"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42980\/revisions"}],"predecessor-version":[{"id":52762,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42980\/revisions\/52762"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}