{"id":14205,"date":"2020-08-05T11:48:06","date_gmt":"2020-08-05T11:48:06","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=14205"},"modified":"2025-03-24T11:42:40","modified_gmt":"2025-03-24T11:42:40","slug":"react-native-hooks-to-build-app","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/react-native-hooks-to-build-app","title":{"rendered":"How to Build a React Native App Using React Native Hooks?"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>React Native has introduced <b>React Native Hooks<\/b>, which are functions to let you hook into <a href=\"https:\/\/www.bacancytechnology.com\/blog\/top-react-ui-component-libraries-and-frameworks\" target=\"_blank\" rel=\"noopener\">React Native state<\/a> and lifecycle features from the function components. Hooks are quite popular in React ecosystem; moreover, hooks have reduced lines of code and developers&#8217; efforts. If you are still not familiar with React hooks, this tutorial will help you understand and explore them.<\/p>\n<h2>Prerequisites<\/h2>\n<ul class=\"bullets text-left\">\n<li>Basic knowledge of React Native<\/li>\n<li>Familiarity with functional and class components, props, state, etc.<\/li>\n<\/ul>\n<p>So, moving forward with the introduction of hooks in React Native.<\/p>\n<h2>Introducing Hooks in React Native<\/h2>\n<p>Hooks are a new feature addition in React Native version 16.8, which allows you to use React Native features without writing a class. These built-in functions let React Native developers use state and lifecycle methods inside functional components. With hooks, the complexity of developing the application is lessened. Keep in mind that hooks don\u2019t work inside classes.<\/p>\n<h2>Why Hooks?<\/h2>\n<p>Here are a few reasons for using hooks in your React Native application.<\/p>\n<ul class=\"bullets\">\n<li>Different ways of doing the same things.<\/li>\n<li>No more complex lifecycle methods.<\/li>\n<li>Simpler code. No more mapStateToProps, mapDispatchToprops with Redux.<\/li>\n<li>Hooks avoid the whole confusion with &#8216;this&#8217; keyword.<\/li>\n<li>Hooks allow you to reuse stateful logic without changing your component hierarchy.<\/li>\n<li>Hooks let us organize the logic inside a component into reusable isolated units.<\/li>\n<\/ul>\n<p>Now let&#8217;s learn how we move from a class to a functional component.<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Are you looking for skilled and proficient React Native developers?<\/span><br \/>\nHere we are to end your search. Contact us today and <a href=\"https:\/\/www.bacancytechnology.com\/hire-react-native-developer\" target=\"_blank\" rel=\"noopener\">hire React Native developer<\/a>. We assure you of the application quality and performance.<\/strong><\/i><\/p>\n<h2>React Native Hooks Lifecycle<\/h2>\n<p>There are three stages of the lifecycle:<\/p>\n<h3>1. Initial Render<\/h3>\n<pre>\r\ngetDerivedStateFromProps\r\n   \t\tuseEffect( ()=>{},[propp1, prop2])\r\n\tcomponentDidMount\r\n\t\tuseEffect( ()=> {},[])\r\n<\/pre>\n<h3>2. Updates<\/h3>\n<pre>\r\ngetDerivedStateFromProps\r\n   \t\tuseEffect( ()=>{},[propp1, prop2])\r\n\r\nshouldComponentUpdate()\r\n\tuseMemo()\r\n\r\ncomponentdidUpdate()\r\n\tuseEffect( ()=>{})\r\n\r\ngetSnapshotBeforeUpdate\r\n\tcustom Hook to hold previous state\r\n<\/pre>\n<h3>3. Unmount<\/h3>\n<pre>\t\r\n\t\tuseEffect( ()=>{return()=>{\/\/cleanup}},[])\r\n<\/pre>\n<p>Thus, we can use hooks to handle states, effects, and context with the help of useState, useEffect, and useContext.<\/p>\n<p>Let\u2019s move towards our next section and dive deeper into hooks. <\/p>\n<h2>Hooks in React Native: Classification:<\/h2>\n<p>There are three basic React Native hooks and seven additional hooks.<\/p>\n<p><strong>Basic Hooks<\/strong><\/p>\n<p>(1) useState<\/p>\n<p>(2) useEffect<\/p>\n<p>(3) useContext<\/p>\n<p><strong>Additional Hooks: <\/strong><\/p>\n<p>(1) useReducer<\/p>\n<p>(2) useCallback<\/p>\n<p>(3) useMemo<\/p>\n<p>(4) useRef<\/p>\n<p>(5) useImperativeHandle<\/p>\n<p>(6) useLayoutEffect<\/p>\n<p>(7) useDebugValue<\/p>\n<h2>React Native Hooks Example<\/h2>\n<p>In this tutorial we will cover useState, useEffect, useContext, useReducer, useMemo, useCallback, and useRef.<\/p>\n<h3>useState<\/h3>\n<p>useState is like this.state() in class. We are going to use useState instead of this.state() to update the state value. Unlike the class, the state is not an object here. We have to initialize the useState() with some value. It can be a number or string, whatever we want. If you have to store multiple values, then for each value, you have to call useState().<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre>Const [data, setData] = useState( \/\/ value )<\/pre>\n<p>The below image would help to understand hooks better.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/React-Natvie-Table-1.png\" alt=\"React Native table\" width=\"1024\" height=\"733\" class=\"alignnone size-full wp-image-30108\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/React-Natvie-Table-1.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/React-Natvie-Table-1-300x215.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/React-Natvie-Table-1-768x550.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>This example declares a state variable name initialized with an empty string. When writing in the text input name, the variable value will be changed.<\/p>\n<p>Also Read: <a href=\"https:\/\/www.bacancytechnology.com\/blog\/how-to-use-redux-with-react-hooks-in-react-native-application\" target=\"_blank\" rel=\"noopener\">How to Use Redux with React Hooks in React Native Application<\/a>?<\/p>\n<h3>useEffect<\/h3>\n<p>useEffect is worked as per class lifecycle <strong>componentDidMount<\/strong>, <strong>componentWillUnMount<\/strong>, <strong>componentdidUpdate<\/strong>. useEffect is just re-render the Component and changing the state variable\u2019s value. uesEffect accepts two arguments and doesn\u2019t return anything.<\/p>\n<p>The first argument is a function that holds the code whatever you want to execute at run time. The second argument is optional when you want to execute the hooks. If you do not pass anything to this argument, your function will be called on mount and every update.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useEffect-min.png\" alt=\"useEffect\" width=\"675\" height=\"899\" class=\"aligncenter size-full wp-image-27529\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useEffect-min.png 675w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useEffect-min-225x300.png 225w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/p>\n<p>The above example shows how we can apply useEffect in functional components. At interval time, the state value will be changed. useEffect() is a side effect used as an event handler to change the state value when you want.<\/p>\n<h3>useContext<\/h3>\n<p>This is another hook for anyone unfamiliar with context API in react native. First, let\u2019s have a look at it. Consider the below image that shows an application has many components.<\/p>\n<p>App Component is our parent component, and within the App component, there are various child components.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/App-component-min.png\" alt=\"App component\" width=\"1100\" height=\"412\" class=\"alignnone size-full wp-image-30109\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/App-component-min.png 1100w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/App-component-min-300x112.png 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/App-component-min-1024x384.png 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/App-component-min-768x288.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>You can see the dependency tree in the image above. Here, we have a limitation of a three-level hierarchy. Imagine if we have a 10 to 20-level hierarchy. In such cases, the useContext hook reduces our efforts from passing props to every level.<\/p>\n<pre>\r\nconst value = useContext(MyContext);\r\n<\/pre>\n<p>Don\u2019t forget that the argument to useContext must be the context object itself. Now let you get some basic knowledge about Additional Hooks.<\/p>\n<h3>useReducer<\/h3>\n<p>useReducer additional hook used as a state management tool. Okay, now don\u2019t we already have useState for state management? Yes, we do have. But, under a few conditions and requirements, it is advisable to use useReducer rather than useState. The next question would be these requirements and the difference between hooks.<\/p>\n<p>To know the difference, let\u2019s have an example. Here we are going to take a counter-example again.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useReducer-min.png\" alt=\"useReducer\" width=\"781\" height=\"943\" class=\"aligncenter size-full wp-image-27530\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useReducer-min.png 781w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useReducer-min-248x300.png 248w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useReducer-min-768x927.png 768w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/p>\n<p>We can handle the different actions at once with useReducer. Run the project; we can increase, decrease, and reset the count value by clicking on the increment, decrement, and reset.<\/p>\n<h4>useReducer vs. useState<\/h4>\n<p>1. Depends on the type of state. If you need to use string, boolean or number value, then choose use useState. And if array or object, then go with the useReducer.<\/p>\n<p>2. The second scenario depends on the number of transitions. If you are updating one or two states, then use useState. And if there are too many updates, then use useReducer.<\/p>\n<p>3. The third one depends on the business logic for the state transition. If you do the complex transition or transfer value old to the new, then better to use useReducer.<\/p>\n<p>4. Fourth is for local or global states. If your state is within the one component, you can use useState, and if your state is at a global level, you must go with useReducer.<\/p>\n<h3>useCallback<\/h3>\n<p>The next hook is useCallback. With each component rendering, functions are recreated. useCallback always returns the memoized version of the function. After using the useCallback function, it will only be changed when one of the dependencies has.<\/p>\n<p>It is useful when some callbacks are passed to the child component, and with that, you can prevent unnecessary renders every time. Like useEffect, useCallback takes a function and an array of dependencies as a parameter. If one of the dependencies&#8217; values changes, the function\u2019s return value will be changed; otherwise, it will always return the cached value.<\/p>\n<p>Assume we have a parent-child component and pass a prop from parent to child component. In such a case, the props within the child component will be called unnecessary whenever the parent component is re-rendered. We can use the useCallback() hook to prevent such unwanted re-renders.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useCallback-min.png\" alt=\"useCallback\" width=\"879\" height=\"999\" class=\"aligncenter size-full wp-image-27532\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useCallback-min.png 879w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useCallback-min-264x300.png 264w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2020\/08\/useCallback-min-768x873.png 768w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/p>\n<p>In the above example, the problem is that all three functions are re-created whenever the counter value is updated. In this case, it is not a big problem to recreate the function unless we don\u2019t have multiple functions. For an application, this might be a big problem in performance for our app. For this problem, we can use useCallback(). So in the above example, we can warp all these functions with useCallback().<\/p>\n<pre>const increment = useCallback(() => { \r\n      setCount(count + 1) \r\n}, [count]) \r\nconst decrement = useCallback(() => { \r\n      setCount(count - 1) \r\n}, [count]) \r\nconst reset = useCallback(O) => { \r\n     resetCount(reset) \r\n}, (reset)\r\n<\/pre>\n<p>So now you click any of the counters, the related state will be changed and re-initialized. for the better and to prevent optimization, we can use the useCallback() hook.<\/p>\n<h3>useMemo<\/h3>\n<p>useMemo() hook is just like useCallback() hook. The difference is that useCallback returns memoized callback, and useMemo returns a memoized value. If you are creating an application that includes extensive data processing, then using the useMemo() hook is a better choice.<\/p>\n<p>So, it will work once at the first render in the application, and then the cached value will be returned every time. If you have userName to pass every time in the application components, then the operation will be done just once with the help of useMemo().<\/p>\n<p>Then state value will be stored in a memoized value, and when you want to use it, you will get it much faster.<\/p>\n<pre>useMemo(()=>{\r\n\tdosomething()\r\n},[dependencies])\r\n<\/pre>\n<p>If you don&#8217;t want to pass arguments, remember to add an empty array as a parameter to useMemo(); otherwise, memoization will not happen. And if you wish to pass arguments, you can also pass them.<\/p>\n<h3>useRef<\/h3>\n<p>useRef is like a container that can store mutable values in its .current property. With Ref, we can access all the DOM nodes and elements and keep a mutable variable. We all know about a ref in React Native.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre>\r\nconst refContainer = useRef(initialValue);\r\n<\/pre>\n<p>Creating createRef in the functional component will create a new instance at every render of DOM. Updating a ref is a side effect. It must be done inside the useEffect() or an event handler. However, the component will not re-render when the useRef value changes. For that, we can use useState().<\/p>\n<h4>Some Rules<\/h4>\n<p<strong>\u201cOnly call Hooks at the top level.\u201d<\/strong><\/p>\n<p>Don\u2019t call hooks inside loops, conditions, or nested functions.<\/p>\n<p><strong>\u201cOnly call Hooks from react-native functions.\u201d<\/strong><\/p>\n<p>Call hooks from React Native Components or Custom Hooks; don\u2019t call them from regular functions.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>If you are looking for assistance in building a React native application with the help of React Native Hooks, then get in touch with us today. We are a globally renowned <a href=\"https:\/\/www.bacancytechnology.com\/react-native-app-development\" target=\"_blank\" rel=\"noopener\">React Native app development company<\/a>, and we have well-versed React Native developers who have top-of-the-line expertise in building React Native app with React Native Hooks. Also, for more such tutorials, you can visit <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/react-native\" target=\"_blank\" rel=\"noopener\">React Native tutorials<\/a> page and explore more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction React Native has introduced React Native Hooks, which are functions to let you hook into React Native state and lifecycle features from the function components. Hooks are quite popular in React ecosystem; moreover, hooks have reduced lines of code and developers&#8217; efforts. If you are still not familiar with React hooks, this tutorial will [&hellip;]<\/p>\n","protected":false},"author":170,"featured_media":14226,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[714],"tags":[],"coauthors":[2430],"class_list":["post-14205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native"],"acf":[],"modified_by":"Binal Prajapati","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/14205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/170"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=14205"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/14205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/14226"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=14205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=14205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=14205"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=14205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}