{"id":2761,"date":"2019-04-30T15:53:50","date_gmt":"2019-04-30T15:53:50","guid":{"rendered":"https:\/\/ionicframework.com\/?p=2761"},"modified":"2020-10-16T19:14:05","modified_gmt":"2020-10-16T19:14:05","slug":"using-react-hooks-in-an-ionic-react-app","status":"publish","type":"post","link":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app","title":{"rendered":"Using React Hooks in Ionic React"},"content":{"rendered":"<blockquote><p>\n  Now available: <a href=\"https:\/\/github.com\/capacitor-community\/react-hooks\" target=\"_blank\" rel=\"noopener\">Capacitor React Hooks!<\/a>\n<\/p><\/blockquote>\n<p>If you&#8217;ve been following the news in the React ecosystem, you&#8217;ve likely heard about the new <a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\">React Hooks API<\/a> available in React v16.8.<\/p>\n<p>Hooks expose React features like state and context to functional, or non-class components. They also make it easier to share &#8220;stateful logic&#8221; between components, such as accessing data in a store, without complex wrapping components.<\/p>\n<p>And now that Ionic supports React (in beta at the time of this writing, <a href=\"https:\/\/ionicframework.com\/announcing-the-ionic-react-beta\/\">try it out!<\/a>), we were excited to see how hooks can make common app building tasks and accessing native APIs really easy and clean, and wanted to walk through the new Hooks APIs in the context of an Ionic React app, including a <a href=\"https:\/\/github.com\/mlynch\/ionic-react-hooks-demo\">real demo app<\/a> that we will dig into at the end of this post.<\/p>\n<p>We&#8217;ll soon see there&#8217;s more to Hooks than it seems!<\/p>\n<p><!--more--><\/p>\n<h2>Stateless Functional Components<\/h2>\n<p>Historically, functional components in React did not manage their own state, as there was no way to access these features outside of classes that extended <code>React.Component<\/code>. This was partly why they were referred to as &#8220;Stateless Functional Components,&#8221; and anything resembling state used in them was seen as a serious code smell (and likely broken).<\/p>\n<p>Let&#8217;s take a simple Stateless Functional Component for example:<\/p>\n<pre><code class=\"language-jsx\">export const MyComponent = ({ name }) =&gt; &lt;h1&gt;My name is {name}&lt;\/h1&gt;;\n\n\/\/ Using the component\n&lt;MyComponent name=&quot;Max&quot; \/&gt;\n<\/code><\/pre>\n<p>In this example, <code>MyComponent<\/code> is a functional component (i.e. it is not a class-based component), but it is also <em>stateless<\/em>, given that it manages none of its own internal state, and <em>pure<\/em> in the sense that it has zero side-effects (i.e. modifications it makes outside of itself, such as writing a file or updating a global variable). Rather, data is provided to the component through <code>props<\/code>, such as <code>name<\/code>, and they are merely rendered out by the component in predictable fashion.<\/p>\n<p>Such limitations made Stateless Functional Components great for creating lots of small, presentational components, which are desirable in many situations. However, that still meant that doing anything more complex required class-based components.<\/p>\n<h2>Adding State to Functional Components<\/h2>\n<p>Hooks completely change what functional components can do in React, bringing state, async operations such as fetch, and APIs like Context to functional components in a safe, possibly even <em>superior<\/em> way (to their class-based counterparts, that is).<\/p>\n<p>To illustrate this, let&#8217;s modify this example to use Hooks to manage a small bit of internal state:<\/p>\n<pre><code class=\"language-jsx\">export const MyComponent = () =&gt; {\n  const [ name, setName ] = useState(&#039;Max&#039;);\n\n  return (\n  &lt;&gt;\n    &lt;h1&gt;My name is {name}&lt;\/h1&gt;\n    &lt;IonInput value={name} onIonChange={(e) =&gt; setName(e.target.value)} \/&gt;\n  &lt;\/&gt;\n  )\n}\n<\/code><\/pre>\n<p>In this example, an <code>IonInput<\/code> is used to type in a name, which is tracked in the internal state for the component and rendered in the <code>&lt;h1&gt;<\/code> tag.<\/p>\n<p>In the first line, we see our first use of Hooks with <code>useState<\/code>. In this case, <code>useState<\/code> <em>hooks<\/em> into the state management features in React, and creates a state variable. <code>useState<\/code> takes an argument for the default value of the state variable, and then returns an array with two values which are <em>destructured<\/em> into two local variables: <code>name<\/code>, and <code>setName<\/code>.<\/p>\n<p>The first value, <code>name<\/code> in this case, is our state variable, and this is the one we render to the <code>&lt;h1&gt;<\/code> element and set as the <code>value<\/code> of the <code>IonInput<\/code>. The second value, <code>setName<\/code> is a function that we call to <em>set<\/em> the state variable&#8217;s value. Both <code>name<\/code> and <code>setName<\/code> in this case can be called whatever we like.<\/p>\n<p>Of course, most components will have many state variables, and thankfully we can call <code>useState<\/code> as many times as we like, one for each variable.<\/p>\n<p>If you&#8217;re thinking that seems like magic because the <code>MyComponent<\/code> function will be called each time the component re-renders and you&#8217;re not sure how React keeps track of all the different <code>useState<\/code> calls, then you&#8217;re on to something. To make this work, React keeps track of the order in which the <code>useState<\/code> calls were made, and thus has strict rules as to where <code>useState<\/code> can be called (for example, calling it in a conditional statement is <em>not<\/em> allowed). To avoid issues, linting tools can help keep your use of <code>useState<\/code> correct, but a good rule of thumb is to keep <code>useState<\/code> calls at the top-level of the function and not nested inside any conditional or nested scopes. Basically, keep it simple!<\/p>\n<h2>Ionic React and React Hooks Example<\/h2>\n<p>Now that we have a basic understanding of hooks and managing a state variable, let&#8217;s take a look at a more involved example of building a login form using Ionic React and React Hooks:<\/p>\n<pre><code class=\"language-jsx\">import React, { useState } from &#039;react&#039;;\n\nimport {\n  IonApp, \n  IonHeader,\n  IonTitle,\n  IonToolbar,\n  IonContent,\n  IonInput,\n  IonList,\n  IonItem,\n  IonLabel,\n  IonButton\n} from &#039;@ionic\/react&#039;;\n\n\nconst LoginForm = () =&gt; {\n  const [ email, setEmail ] = useState(&#039;&#039;);\n  const [ password, setPassword ] = useState(&#039;&#039;);\n\n  const [ formErrors, setFormErrors ] = useState({});\n\n  const submit = async () =&gt; {\n    try {\n      await login({\n        email,\n        password\n      });\n    } catch (e) {\n      setFormErrors(e);\n    }\n  }\n\n  return (\n    &lt;&gt;\n      &lt;IonHeader&gt;\n        &lt;IonToolbar&gt;\n          &lt;IonTitle&gt;\n            Login\n          &lt;\/IonTitle&gt;\n        &lt;\/IonToolbar&gt;\n      &lt;\/IonHeader&gt;\n      &lt;IonContent&gt;\n        &lt;form onSubmit={(e) =&gt; { e.preventDefault(); submit();}}&gt;\n          &lt;div&gt;\n            {formErrors ? (\n              formErrors.message\n            ): null}\n          &lt;\/div&gt;\n          &lt;IonList&gt;\n            &lt;IonItem&gt;\n              &lt;IonLabel&gt;Email&lt;\/IonLabel&gt;\n              &lt;IonInput name=&quot;email&quot; type=&quot;email&quot; value={email} onIonChange={(e) =&gt; setEmail(e.target.value)}\/&gt;\n            &lt;\/IonItem&gt;\n            &lt;IonItem&gt;\n              &lt;IonLabel&gt;Password&lt;\/IonLabel&gt;\n              &lt;IonInput name=&quot;password&quot; type=&quot;password&quot; value={email} onIonChange={(e) =&gt; setPassword(e.target.value)}\/&gt;\n            &lt;\/IonItem&gt;\n          &lt;\/IonList&gt;\n\n          &lt;IonButton expand={true} type=&quot;submit&quot;&gt;Log in&lt;\/IonButton&gt;\n        &lt;\/form&gt;\n      &lt;\/IonContent&gt;\n    &lt;\/&gt;\n  )\n}\n\n<\/code><\/pre>\n<h2>What about TypeScript?<\/h2>\n<p>Before we move on, you may have wondered in the above examples (which are plain JS), how <code>useState<\/code> and other hooks work with TypeScript. Thankfully, <code>useState<\/code> is a generic function which can take type arguments if they can&#8217;t be inferred.<\/p>\n<p>For example, if we had a type of <code>Car<\/code> that we wanted to set in state, we could call it this way:<\/p>\n<pre><code class=\"language-jsx\">const [ car, setCar ] = useState&lt;Car&gt;({ color: &#039;red&#039; })\n<\/code><\/pre>\n<p>Hooks work great with TypeScript-based React apps!<\/p>\n<h2>Ionic React with a Class-Based React Component<\/h2>\n<p>The above examples are fun, and Hooks are certainly a quirky, curious new API that is oddly pleasing to use. However, one of the reasons they&#8217;ve practically blown up in the React community are because of the code simplicity benefits they bring.<\/p>\n<p>To illustrate that, let&#8217;s build the same example above but using the traditional React Class-based component approach:<\/p>\n<pre><code class=\"language-jsx\">import React, { useState, FormEvent } from &#039;react&#039;;\n\nimport {\n  IonHeader,\n  IonToolbar,\n  IonTitle,\n  IonContent,\n  IonList,\n  IonItem,\n  IonLabel,\n  IonInput,\n  IonButton\n} from &quot;@ionic\/react&quot;;\n\nexport class LoginPage extends React.Component {\n  constructor(props) {\n    super(props);\n\n    this.state = {\n      email: &#039;&#039;,\n      password: &#039;&#039;\n    }\n  }\n\n  async handleSubmit(e: FormEvent) {\n    e.preventDefault();\n\n    try {\n      const user = await login(email, password);\n\n      \/\/ ...\n    } catch (e) {\n      console.error(e);\n    }\n  }\n\n  handleInputChange(e) {\n    this.setState({\n      [e.target.name]: e.target.value\n    });\n  }\n\n  render() {\n    return (\n    &lt;&gt;\n      &lt;IonHeader&gt;\n        &lt;IonToolbar color=&quot;primary&quot;&gt;\n          &lt;IonTitle&gt;Login&lt;\/IonTitle&gt;\n        &lt;\/IonToolbar&gt;\n      &lt;\/IonHeader&gt;\n      &lt;IonContent&gt;\n        &lt;form onSubmit={e =&gt; this.handleSubmit(e)} action=&quot;post&quot;&gt;\n          &lt;IonList&gt;\n            &lt;IonItem&gt;\n              &lt;IonLabel&gt;Email&lt;\/IonLabel&gt;\n              &lt;IonInput  type=&quot;email&quot; value={email} onInput={(e: any) =&gt; this.handleInputChange(e)} \/&gt;\n            &lt;\/IonItem&gt;\n            &lt;IonItem&gt;\n              &lt;IonLabel&gt;Password&lt;\/IonLabel&gt;\n              &lt;IonInput type=&quot;password&quot; value={password} onInput={(e: any) =&gt; this.handleInputChange(e)} \/&gt;\n            &lt;\/IonItem&gt;\n            &lt;IonButton type=&quot;submit&quot;&gt;Log in&lt;\/IonButton&gt;\n          &lt;\/IonList&gt;\n        &lt;\/form&gt;\n      &lt;\/IonContent&gt;\n    &lt;\/&gt;\n    );\n  }\n}\n<\/code><\/pre>\n<p>In the above example, you&#8217;ll notice a few hallmarks of class-based components: a constructor, calling <code>this.state<\/code>, having to capture <code>this<\/code> in callback handlers (in the above example we&#8217;ve used the arrow function approach for event handlers, but many use <code>this.eventHandler = this.eventHandler.bind(this)<\/code> which has some serious gotchas).<\/p>\n<p>While this example isn&#8217;t very complicated, it&#8217;s enough to show that this component is simpler as a functional component using Hooks than its class-based counterpart (though some may prefer the boilerplate of the class-based method, perhaps Java developers in another life).<\/p>\n<h2>Components with Side Effects: useEffect<\/h2>\n<p>State variables are just one use case for Hooks. Many components will need to do things that are considered &#8220;side effects&#8221; after a component is rendered (such as on mount or update). A side effect is any operation that causes something outside of the component to be modified as a side effect of using this Component. For example, making an API request is a side-effect that many Components need to perform.<\/p>\n<p>This is where <code>useEffect<\/code> comes in. For example, let&#8217;s say we need to fetch some data from our component when it mounts by making a request to our API:<\/p>\n<pre><code class=\"language-jsx\">const MyComponent: = () =&gt; {\n  const [data, setData] = useState({});\n\n  useEffect(() =&gt; {\n    async function loadData() {\n      const loadedData = await getDataFromAPI();\n      setData(loadedData);\n    }\n\n    loadData();\n  }, []);\n\n  const items = (data.items || []);\n\n  return (\n    &lt;div&gt;\n      There are {items.length} entries\n    &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n<p>For class-based components, data fetching was often done in a lifecycle method such as <code>componentDidMount<\/code>, and at first it&#8217;s not obvious how calling <code>useEffect<\/code> in the above works in comparison.<\/p>\n<p>You can think of <code>useEffect<\/code> as a combination of the lifecycle methods <code>componentDidMount<\/code>, <code>componentDidUpdate<\/code>, and <code>componentWillUnmount<\/code>, given that it first runs as soon as the component is mounted and has rendered, will run every time the component is updated, and can run cleanup when the component will be unmounted.<\/p>\n<p>However, in the above, we wouldn&#8217;t want to fetch our data after every update! That could mean thousands of redundant API requests if a component is updated many times in short succession. Instead, <code>useEffect<\/code> takes an extra argument of <em>dependencies<\/em>: <code>useEffect(effectCallack, dependencyList)<\/code>. In <code>dependencyList<\/code>, you can tell the effect to run only after certain state variables have changed, or pass an empty array to only allow the effect to run the first time the component is mounted.<\/p>\n<p>In the above, we pass <code>[]<\/code> as the dependency list so that our effect only runs the first time the component is mounted.<\/p>\n<p>One note: <code>useEffect<\/code> is only necessary if you wish to perform the side-effects relative to renders of the component. If, instead, you wish to make an API request after an action (such as a button click in your component), just make the fetch normally and call the corresponding setter function for your state variable when data is returned and you wish to update the component. In this sense, <code>useEffect<\/code> is a confusing name as you can incorporate side-effects in the component without needing to use it.<\/p>\n<h2>Easy state management with useContext<\/h2>\n<p>Most React developers know the struggle of trying to share global state across their application. For better or worse, this struggle has caused many developers to look at powerful solutions like Redux that were overkill for their needs, when something much simpler would have sufficed.<\/p>\n<p>Well, with Hooks, <a href=\"https:\/\/reactjs.org\/docs\/context.html\">Context<\/a>, and the <code>useContext<\/code> API, this struggle is effectively over. Accessing a global Context instance with <code>useContext<\/code> makes it possible to do rudimentary state management or easily <a href=\"https:\/\/medium.com\/simply\/state-management-with-react-hooks-and-context-api-at-10-lines-of-code-baf6be8302c\">create your own mini Redux<\/a> with no external dependencies and a simpler architecture.<\/p>\n<p>We will walk through the <code>useContext<\/code> hook in the example app at the end.<\/p>\n<h2>Custom hooks<\/h2>\n<p>React comes with a number of hooks out of the box, but they are useful for far more than just state management or accessing context!<\/p>\n<p>In the following sections we will take a look at some custom hooks and how they can bring big benefits to React and Ionic app development.<\/p>\n<h2>Native APIs with Ionic and React Hooks<\/h2>\n<p>Because hooks are perfect for reusable, stateful logic, maybe they would be perfect for plugins that access Native APIs on iOS, Android, Electron, and the browser? Turns out they are, and we can build or use custom hooks to do just that!<\/p>\n<p>Imagine accessing Geolocation APIs on the device. A custom hook called <code>useGeolocation<\/code> might automatically listen for geolocation position changes and update a state variable:<\/p>\n<pre><code class=\"language-jsx\">const MyApp = () =&gt; {\n  const pos = useGeolocation();\n\n  return (\n    &lt;span&gt;Lat: {pos.lat}, Lng: {pos.lng}&lt;\/span&gt;\n  );\n}\n<\/code><\/pre>\n<p>This example shows the hidden power of Hooks. With just one line, we have set up a geolocation query on component mount, which starts a watch that will update when our position changes, which will then update a state variable, which will cause the component to re-render and the updated position be displayed.<\/p>\n<p>Now, imagine doing the same for other Native fetures like Camera, Storage, Photos, or Barcode Scanning, and you can get a sense for how easy hooks make interacting with these kinds of APIs.<\/p>\n<p>So, how does this pertain to Ionic? Well, as Ionic React gets off the ground, we are exploring doing a set of hooks for the APIs available in <a href=\"https:\/\/capacitorjs.com\">Capacitor<\/a> which we think will be pretty awesome!<\/p>\n<h2>An example app<\/h2>\n<p>With the introduction to hooks out of the way, let&#8217;s take a look at a simple Ionic React app that uses a number of the hooks above, the Puppers app (source code <a href=\"https:\/\/github.com\/mlynch\/ionic-react-hooks-demo\">here<\/a>):<\/p>\n<p><a href=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"172\" height=\"300\" data-src=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-172x300.png\" alt=\"\" class=\"aligncenter size-medium wp-image-2763 lazyload\" data-srcset=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-172x300.png 172w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-768x1337.png 768w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-588x1024.png 588w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1.png 840w\" data-sizes=\"auto, (max-width: 172px) 100vw, 172px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 172px; --smush-placeholder-aspect-ratio: 172\/300;\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"172\" height=\"300\" src=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-172x300.png\" alt=\"\" class=\"aligncenter size-medium wp-image-2763\" srcset=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-172x300.png 172w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-768x1337.png 768w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1-588x1024.png 588w, https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/ss1.png 840w\" sizes=\"auto, (max-width: 172px) 100vw, 172px\" \/><\/noscript><\/a><\/p>\n<p>This app fetches a list of random images of adorable and Very Good puppers from the <a href=\"https:\/\/dog.ceo\/\">Dog API<\/a>, with a few features that are completely overkill but also just right, including persisting the last images to localStorage, and a mini-implementation of redux for managing state through the Context API using just the <code>useReducer<\/code> hook (and no external dependencies!). There is also a custom hook called <code>useLocalStorage<\/code> that automatically loads and persists a key and value to localStorage (ported from <a href=\"http:\/\/usehooks.com\">usehooks.com<\/a>).<\/p>\n<p>This demo app also shows how to use Ionic React in a plain JS app without TypeScript.<\/p>\n<h4>App.js<\/h4>\n<p>In <a href=\"https:\/\/github.com\/mlynch\/ionic-react-hooks-demo\/blob\/master\/src\/App.js\">App.js<\/a>, we have our main App component at the bottom of the file:<\/p>\n<pre><code class=\"language-jsx\">const App = () =&gt; {\n  return (\n    &lt;IonApp&gt;\n      &lt;AppContextProvider&gt;\n        &lt;Puppers \/&gt;\n      &lt;\/AppContextProvider&gt;\n    &lt;\/IonApp&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n<p>This component creates an <code>IonApp<\/code> element, and then wraps the contents of the app with an <code>AppContextProvider<\/code> which will be our main <code>Context<\/code> for managing global state. Inside of that component, the <code>Puppers<\/code> page is rendered. Pretty basic, and if you aren&#8217;t familiar with the Context API, make sure to <a href=\"https:\/\/reactjs.org\/docs\/context.html\">read more about it<\/a> before continuing.<\/p>\n<p>Next, we have the <code>AppContextProvider<\/code>:<\/p>\n<pre><code class=\"language-jsx\">const AppContext = createContext();\n\nconst AppContextProvider = (props) =&gt; {\n  const [data, setData] = useLocalStorage(&#039;data&#039;, initialState);\n\n  let [state, dispatch] = useReducer(reducer, data);\n\n  let value = { state, dispatch };\n\n  useEffect(() =&gt; {\n    setData(state);\n  }, [state, setData]);\n\n  return (\n    &lt;AppContext.Provider value={value}&gt;{props.children}&lt;\/AppContext.Provider&gt;\n  );\n}\n<\/code><\/pre>\n<p>This one is way more complex and uses a number of hooks and even a custom hook! Let&#8217;s walk through some of it:<\/p>\n<p>The first line calls our custom <code>useLocalStorage<\/code> hook, which will load and automatically persist values to the <code>data<\/code> item in localStorage:<\/p>\n<pre><code class=\"language-js\">const [data, setData ] = useLocalStorage(&#039;data&#039;, initialState);\n<\/code><\/pre>\n<p>Next, we create a reducer using <code>useReducer<\/code> that takes a reducer function and an initial value, which we will pass in the <code>data<\/code> state value from <code>useLocalStorage<\/code>. This will cause the reducer to use any data loaded from localStorage as its initial value! If you haven&#8217;t used redux before, <code>useReducer<\/code> will likely be weird at first. However, it&#8217;s a useful utility for complex state logic and lets us manage a single global state object that can be modified by actions in the application. Our application only has one action but you can imagine the average application having hundreds of actions. Read more about <a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usereducer\">useReducer<\/a>.<\/p>\n<pre><code class=\"language-js\">let [state, dispatch] = useReducer(reducer, data);\n<\/code><\/pre>\n<p>And our reducer function is very basic:<\/p>\n<pre><code class=\"language-js\">const reducer = (state, action) =&gt; {\n  if (action.type === &#039;setPuppers&#039;) {\n    return { ...state, puppers: action.puppers }\n  }\n  return state;\n}\n<\/code><\/pre>\n<p>If this is confusing, hopefully seeing a component &#8220;use&#8221; the above context and reducer should make it more clear:<\/p>\n<h4>Puppers.js<\/h4>\n<p>Let&#8217;s take a look at the Puppers Component, which loops through the list of puppers from the API and renders them one by adorable one:<\/p>\n<pre><code class=\"language-jsx\">export const Puppers = () =&gt; {\n  const { state, dispatch } = useContext(AppContext);\n\n  const fetchPuppers = useCallback(async () =&gt; {\n    const ret = await fetch(&#039;https:\/\/dog.ceo\/api\/breeds\/image\/random\/10&#039;);\n    const json = await ret.json();\n    dispatch({\n      type: &#039;setPuppers&#039;,\n      puppers: json.message\n    })\n  }, [dispatch]);\n\n  useEffect(() =&gt; {\n    fetchPuppers();\n  }, [fetchPuppers]);\n\n  return (\n  &lt;&gt;\n    &lt;IonHeader&gt;\n      &lt;IonToolbar&gt;\n        &lt;IonTitle&gt;Puppers&lt;\/IonTitle&gt;\n        &lt;IonButtons slot=&quot;end&quot;&gt;\n          &lt;IonButton onClick={() =&gt; fetchPuppers()}&gt;\n            &lt;IonIcon icon=&quot;refresh&quot; \/&gt;\n          &lt;\/IonButton&gt;\n        &lt;\/IonButtons&gt;\n      &lt;\/IonToolbar&gt;\n    &lt;\/IonHeader&gt;\n    &lt;IonContent&gt;\n      {state.puppers.map(pupper =&gt; {\n        return (\n          &lt;IonCard key={pupper}&gt;\n            &lt;IonCardContent&gt;\n              &lt;img src={pupper} \/&gt;\n            &lt;\/IonCardContent&gt;\n          &lt;\/IonCard&gt;\n        )\n      })}\n    &lt;\/IonContent&gt;\n  &lt;\/&gt;\n  );\n}\n<\/code><\/pre>\n<p>Let&#8217;s take this line by line. The first line accesses the <code>AppContext<\/code> that we instantiated using the <code>&lt;AppContextProvider&gt;<\/code> component in our <code>App<\/code> component, specifically the <code>value<\/code> of the provider:<\/p>\n<pre><code class=\"language-js\">const { state, dispatch } = useContext(AppContext);\n<\/code><\/pre>\n<p>The <code>state<\/code> variable will contain our global state in the context, and the <code>dispatch<\/code> variable is a function we can call to send an action to our reducer (to update our state, for example).<\/p>\n<p>Next, we define a function that we can use to call our API:<\/p>\n<pre><code class=\"language-js\">const fetchPuppers = useCallback(async() =&gt; {\n  const ret = await fetch(&#039;https:\/\/dog.ceo\/api\/breeds\/image\/random\/10&#039;);\n  const json = await ret.json();\n  dispatch({\n    type: &#039;setPuppers&#039;,\n    puppers: json.message\n  })\n}, [dispatch]);\n<\/code><\/pre>\n<p>Since we&#8217;re going to call <code>fetchPuppers<\/code> from a few different places in our component, we use the <code>useCallback<\/code> hook to make sure the Hooks API properly understands the dependencies this function has. This was a solution to sharing a function in several hooks provided by Dan Abramov on his <a href=\"https:\/\/overreacted.io\/a-complete-guide-to-useeffect\/\">Complete Guide to useEffect<\/a>, though there are alternative ways to achieve this. We provide the <code>dispatch<\/code> function as a dependency to our <code>fetchPuppers<\/code> call, as it will be called with fresh puppers once the response returns.<\/p>\n<p>Next, we use <code>useEffect<\/code> with an empty dependency list (i.e. <code>[]<\/code> as the last argument) to make a fetch as soon as this component is mounted:<\/p>\n<pre><code class=\"language-js\">useEffect(() =&gt; {\n  fetchPuppers();\n}, [fetchPuppers]);\n<\/code><\/pre>\n<p>Finally, we render our component, and loop through each pupper, rendering them to the screen:<\/p>\n<pre><code class=\"language-jsx\">return (\n&lt;&gt;\n  &lt;IonHeader&gt;\n    &lt;IonToolbar&gt;\n      &lt;IonTitle&gt;Puppers&lt;\/IonTitle&gt;\n      &lt;IonButtons slot=&quot;end&quot;&gt;\n        &lt;IonButton onClick={() =&gt; fetchPuppers()}&gt;\n          &lt;IonIcon icon=&quot;refresh&quot; \/&gt;\n        &lt;\/IonButton&gt;\n      &lt;\/IonButtons&gt;\n    &lt;\/IonToolbar&gt;\n  &lt;\/IonHeader&gt;\n  &lt;IonContent&gt;\n    {state.puppers.map(pupper =&gt; {\n      return (\n        &lt;IonCard key={pupper}&gt;\n          &lt;IonCardContent&gt;\n            &lt;img src={pupper} \/&gt;\n          &lt;\/IonCardContent&gt;\n        &lt;\/IonCard&gt;\n      )\n    })}\n  &lt;\/IonContent&gt;\n&lt;\/&gt;\n);\n<\/code><\/pre>\n<p>A few things to see here: first, notice the <code>onClick<\/code> event in the button in the toolbar. This will make a new fetch to the API, get 10 more random puppers, which will then cause global state to update, and our component to re-render.<\/p>\n<p>Finally, given that we are using global state instead of local state, when we render out each pupper, we are accessing the <code>state.puppers<\/code> field which came from the initial <code>useContext<\/code> call.<\/p>\n<p>And that&#8217;s it!<\/p>\n<h2>Where to go from here<\/h2>\n<p>Despite React Hooks being very new, the community has created a plethora of interesting Hooks. One such library, <a href=\"https:\/\/streamich.github.io\/react-use\/\">react-use<\/a>, has some simple yet powerful hooks such as <a href=\"https:\/\/streamich.github.io\/react-use\/?path=\/story\/ui-usevideo--docs\">useVideo<\/a> (for easily interacting with an HTML5 video element). I personally love how clean and simple Hooks make interacting with stateful controls such as HTML5 media elements and APIs like localStorage.<\/p>\n<p>Also, make sure to watch the <a href=\"https:\/\/youtu.be\/dpw9EHDh2bM?t=698\">React Conf Hooks Keynote Announcement<\/a> by Dan Abramov, along with his great blog posts that dig into hooks in more detail, such as <a href=\"https:\/\/overreacted.io\/a-complete-guide-to-useeffect\/\">A Complete Guide to useEffect<\/a>.<\/p>\n<p>Finally, keep an eye out for some awesome hooks stuff from the Ionic team specifically for Ionic React apps (using Capacitor for native functionality). We love hooks and think they will make building apps considerably easier. And, if you haven&#8217;t tried the <a href=\"https:\/\/ionicframework.com\/announcing-the-ionic-react-beta\/\">Ionic React beta<\/a> give it a shot and let us know what you think!<\/p>\n<p>Any questions on using Hooks and how they might be useful in Ionic React apps specifically? Leave a comment below and we&#8217;ll try to help!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now available: Capacitor React Hooks! If you&#8217;ve been following the news in the React ecosystem, you&#8217;ve likely heard about the new React Hooks API available in React v16.8. Hooks expose React features like state and context to functional, or non-class components. They also make it easier to share &#8220;stateful logic&#8221; between components, such as accessing [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2765,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"publish_to_discourse":"","publish_post_category":"","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"","discourse_permalink":"","wpdc_publishing_response":"","wpdc_publishing_error":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[121],"tags":[136,149],"class_list":["post-2761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering","tag-react","tag-react-hooks"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.0 (Yoast SEO v23.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using React Hooks in Ionic React - Ionic Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using React Hooks in Ionic React\" \/>\n<meta property=\"og:description\" content=\"Now available: Capacitor React Hooks! If you&#8217;ve been following the news in the React ecosystem, you&#8217;ve likely heard about the new React Hooks API available in React v16.8. Hooks expose React features like state and context to functional, or non-class components. They also make it easier to share &#8220;stateful logic&#8221; between components, such as accessing [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\" \/>\n<meta property=\"og:site_name\" content=\"Ionic Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-30T15:53:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-10-16T19:14:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1440\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Max Lynch\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@maxlynch\" \/>\n<meta name=\"twitter:site\" content=\"@ionicframework\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Max Lynch\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#article\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\"},\"author\":{\"name\":\"Max Lynch\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e\"},\"headline\":\"Using React Hooks in Ionic React\",\"datePublished\":\"2019-04-30T15:53:50+00:00\",\"dateModified\":\"2020-10-16T19:14:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\"},\"wordCount\":2500,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png\",\"keywords\":[\"react\",\"react hooks\"],\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\",\"url\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\",\"name\":\"Using React Hooks in Ionic React - Ionic Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ionic.io\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png\",\"datePublished\":\"2019-04-30T15:53:50+00:00\",\"dateModified\":\"2020-10-16T19:14:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png\",\"width\":1440,\"height\":800,\"caption\":\"Ionic React Hooks\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ionic.io\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using React Hooks in Ionic React\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ionic.io\/blog\/#website\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"name\":\"ionic.io\/blog\",\"description\":\"Build amazing native and progressive web apps with the web\",\"publisher\":{\"@id\":\"https:\/\/ionic.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ionic.io\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/ionic.io\/blog\/#organization\",\"name\":\"Ionic\",\"url\":\"https:\/\/ionic.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png\",\"width\":1920,\"height\":854,\"caption\":\"Ionic\"},\"image\":{\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/ionicframework\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e\",\"name\":\"Max Lynch\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg\",\"contentUrl\":\"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg\",\"caption\":\"Max Lynch\"},\"description\":\"CEO\",\"sameAs\":[\"http:\/\/twitter.com\/maxlynch\",\"https:\/\/x.com\/maxlynch\"],\"url\":\"https:\/\/ionic.io\/blog\/author\/max\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Using React Hooks in Ionic React - Ionic Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app","og_locale":"en_US","og_type":"article","og_title":"Using React Hooks in Ionic React","og_description":"Now available: Capacitor React Hooks! If you&#8217;ve been following the news in the React ecosystem, you&#8217;ve likely heard about the new React Hooks API available in React v16.8. Hooks expose React features like state and context to functional, or non-class components. They also make it easier to share &#8220;stateful logic&#8221; between components, such as accessing [&hellip;]","og_url":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app","og_site_name":"Ionic Blog","article_published_time":"2019-04-30T15:53:50+00:00","article_modified_time":"2020-10-16T19:14:05+00:00","og_image":[{"width":1440,"height":800,"url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","type":"image\/png"}],"author":"Max Lynch","twitter_card":"summary_large_image","twitter_creator":"@maxlynch","twitter_site":"@ionicframework","twitter_misc":{"Written by":"Max Lynch","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#article","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app"},"author":{"name":"Max Lynch","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e"},"headline":"Using React Hooks in Ionic React","datePublished":"2019-04-30T15:53:50+00:00","dateModified":"2020-10-16T19:14:05+00:00","mainEntityOfPage":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app"},"wordCount":2500,"commentCount":0,"publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"image":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage"},"thumbnailUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","keywords":["react","react hooks"],"articleSection":["Engineering"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app","url":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app","name":"Using React Hooks in Ionic React - Ionic Blog","isPartOf":{"@id":"https:\/\/ionic.io\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage"},"image":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage"},"thumbnailUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","datePublished":"2019-04-30T15:53:50+00:00","dateModified":"2020-10-16T19:14:05+00:00","breadcrumb":{"@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#primaryimage","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","width":1440,"height":800,"caption":"Ionic React Hooks"},{"@type":"BreadcrumbList","@id":"https:\/\/ionic.io\/blog\/using-react-hooks-in-an-ionic-react-app#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ionic.io\/blog"},{"@type":"ListItem","position":2,"name":"Using React Hooks in Ionic React"}]},{"@type":"WebSite","@id":"https:\/\/ionic.io\/blog\/#website","url":"https:\/\/ionic.io\/blog\/","name":"ionic.io\/blog","description":"Build amazing native and progressive web apps with the web","publisher":{"@id":"https:\/\/ionic.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ionic.io\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ionic.io\/blog\/#organization","name":"Ionic","url":"https:\/\/ionic.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2020\/10\/white-on-color.png","width":1920,"height":854,"caption":"Ionic"},"image":{"@id":"https:\/\/ionic.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/ionicframework"]},{"@type":"Person","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/91f360cffbd804a464b0c4a87b5c5f1e","name":"Max Lynch","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ionic.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg","contentUrl":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2018\/05\/max-avatar-150x150.jpg","caption":"Max Lynch"},"description":"CEO","sameAs":["http:\/\/twitter.com\/maxlynch","https:\/\/x.com\/maxlynch"],"url":"https:\/\/ionic.io\/blog\/author\/max"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/ionic.io\/blog\/wp-content\/uploads\/2019\/04\/react-hooks.png","_links":{"self":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/2761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/comments?post=2761"}],"version-history":[{"count":0,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/posts\/2761\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media\/2765"}],"wp:attachment":[{"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/media?parent=2761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/categories?post=2761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ionic.io\/blog\/wp-json\/wp\/v2\/tags?post=2761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}