<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.1.1">Jekyll</generator><link href="https://codewithnico.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://codewithnico.com/" rel="alternate" type="text/html" /><updated>2022-09-09T15:51:25+00:00</updated><id>https://codewithnico.com/feed.xml</id><title type="html">CodeWithNico</title><subtitle>Sharing my journey about building production ready React apps</subtitle><author><name>Nico</name><email>ngomez@hey.com</email></author><entry xml:lang="es"><title type="html">¿Qué es lo mínimo que debes saber para considerarte full stack developer?</title><link href="https://codewithnico.com/lo-minimo-que-debes-saber-para-ser-full-stack/" rel="alternate" type="text/html" title="¿Qué es lo mínimo que debes saber para considerarte full stack developer?" /><published>2021-01-08T23:00:00+00:00</published><updated>2021-01-08T23:00:00+00:00</updated><id>https://codewithnico.com/lo-minimo-que-debes-saber-para-ser-full-stack</id><content type="html" xml:base="https://codewithnico.com/lo-minimo-que-debes-saber-para-ser-full-stack/">&lt;p&gt;Muchos dicen que para ser full stack tienes que saber mil cosas e incluso que los desarrolladores full stack no existen (o que son unicornios 🦄).&lt;/p&gt;

&lt;p&gt;La razón de esto es que los full stack se mueven en distintas capas de un proyecto de software: en lo relacionado a interfaz de usuario (frontend), en la parte de procesamiento y entrega de datos (backend), entre otras cosas. Y para dominar cada una, se necesita manejar muchas herramientas.&lt;/p&gt;

&lt;p&gt;Como cada mundo involucra miles de tecnologías, ahora nos hacemos la pregunta: &lt;strong&gt;¿qué es lo mínimo que debe saber un desarrollar para considerarse full stack?&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;definición-de-un-desarrollador-full-stack&quot;&gt;Definición de un desarrollador full stack&lt;/h2&gt;

&lt;p&gt;Antes es importante ponernos de acuerdo de qué es un desarrollador full stack.&lt;/p&gt;

&lt;p&gt;Muchos piensan que un full stack debe dominar todas las tecnologías de &lt;em&gt;frontend&lt;/em&gt; y de &lt;em&gt;backend&lt;/em&gt;. Algo difícil de lograr…&lt;/p&gt;

&lt;p&gt;A mí me gusta usar una definición más amplia:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Desarrollador full stack:&lt;/strong&gt; Alguien que es capaz de crear una aplicación que involucre interacción con usuarios finales y también procesamiento y entrega de datos.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Esta definición tiene 2 consecuencias:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Se basa en lo que &lt;strong&gt;una persona es capaz de lograr&lt;/strong&gt; y no a lo que se supone que sabe.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Las tecnologías y herramientas quedan en segundo plano&lt;/strong&gt;, a servicio del profesional. Tal y como debe ser.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;ahora-sí-lo-mínimo-que-tienes-que-saber-para-considerarte-full-stack&quot;&gt;Ahora sí… Lo mínimo que tienes que saber para considerarte full stack&lt;/h2&gt;

&lt;p&gt;Debido a la definición anterior, tienes que saber crear aplicaciones que involucren el mundo del &lt;em&gt;frontend&lt;/em&gt; (interfaz de usuario) y del &lt;em&gt;backend&lt;/em&gt; (entrega y procesamiento de datos).&lt;/p&gt;

&lt;p&gt;Como son muchas aristas y tecnologías, el full stack es capaz de complementar los conocimientos que tenga más débiles o que no le interesen.&lt;/p&gt;

&lt;p&gt;Hay muchas combinaciones que logran esto:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;En vez de aprender sobre servidores, devOps y sistemas operativos (donde colocas tu aplicación “backend”) puedes aprender una herramienta tipo &lt;a href=&quot;https://heroku.com&quot;&gt;Heroku&lt;/a&gt; que hace todo esto por ti.&lt;/li&gt;
  &lt;li&gt;En vez de aprender a crear backend complejos desde 0 puedes utilizar un framework con muchas cosas ya hechas (&lt;a href=&quot;https://rubyonrails.org/&quot;&gt;Ruby on rails&lt;/a&gt;, &lt;a href=&quot;https://www.djangoproject.com/&quot;&gt;Django&lt;/a&gt;, &lt;a href=&quot;https://laravel.com/&quot;&gt;Laravel&lt;/a&gt;) o incluso puedes usar “backend as a service” tipo &lt;a href=&quot;http://firebase.com/&quot;&gt;Firebase&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;En vez de aprender a hacer frameworks frontend desde 0 puedes usar herramientas ya existentes como &lt;a href=&quot;https://angular.io/&quot;&gt;Angular&lt;/a&gt;, &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue&lt;/a&gt; o &lt;a href=&quot;https://vuejs.org/&quot;&gt;React&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;En vez de aprender CSS puedes usar templates ya hechos.&lt;/li&gt;
  &lt;li&gt;En vez de aprender a desarrollar en Android (Java/Kotlin) e iPhone (Swift) puedes usar &lt;a href=&quot;https://reactnative.dev/&quot;&gt;React native&lt;/a&gt; (javascript) o &lt;a href=&quot;https://flutter.dev/&quot;&gt;Flutter&lt;/a&gt; (Dart) para crear plataformas en ambas plataformas.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Esto nos abre las puertas a muchos perfiles interesantes.&lt;/p&gt;

&lt;h2 id=&quot;ejemplos-de-perfiles-de-desarrollador-full-stack&quot;&gt;Ejemplos de perfiles de desarrollador full stack&lt;/h2&gt;

&lt;p&gt;Algunos ejemplos de perfiles full stack:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Javascript con jQuery + Firebase&lt;/li&gt;
  &lt;li&gt;Ruby on rails en AWS on react&lt;/li&gt;
  &lt;li&gt;React Native con Django en un VPS&lt;/li&gt;
  &lt;li&gt;React native con Nodejs sobre Heroku&lt;/li&gt;
  &lt;li&gt;Vue con Laravel en docker sobre AWS&lt;/li&gt;
  &lt;li&gt;AWS Amplify&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mi-elección-personal&quot;&gt;Mi elección personal&lt;/h2&gt;

&lt;p&gt;Yo me considero un desarrollador full stack por que he hecho varias aplicaciones que involucran ambos mundos de manera exitosa.&lt;/p&gt;

&lt;p&gt;Para que tengas una referencia, mis elecciones tecnológicas son:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Backend&lt;/em&gt;: me considero buen backend con Ruby on Rails (RoR). Lo aprendí mientras estudiaba y desde ahí que quedé enamorado del framework. También manejo bien node.js, pero mi elección casi siempre es RoR.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Frontend&lt;/em&gt;: por temas de trabajo tuve que aprender React. No me encanta, pero hace bien su trabajo.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;UI&lt;/em&gt;: no soy experto en CSS. Por eso, suelo utilizar frameworks de CSS, como por ejemplo Bootstrap o AntDesign.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Devops&lt;/em&gt;: nunca me gustó lo relacionado con servidores. Así que mi elección es utilizar Heroku.&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="es" /><category term="programacion" /><category term="carrera" /><summary type="html">Desmitificando al full stack 🦄</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2021-01-11-lo-minimo-que-debes-saber-para-ser-full-stack/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2021-01-11-lo-minimo-que-debes-saber-para-ser-full-stack/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="es"><title type="html">Solución a problema de 404 de React en Netlify</title><link href="https://codewithnico.com/problema-404-netlify-react/" rel="alternate" type="text/html" title="Solución a problema de 404 de React en Netlify" /><published>2021-01-08T15:00:00+00:00</published><updated>2021-01-08T15:00:00+00:00</updated><id>https://codewithnico.com/problema-404-netlify-react</id><content type="html" xml:base="https://codewithnico.com/problema-404-netlify-react/">&lt;p&gt;Tu APP React con react-router funciona perfecto en localhost, pero cuando la subes a Netlify tienes problemas con las rutas.&lt;/p&gt;

&lt;p&gt;El problema ocurre cuando intentas entrar a una ruta particular. Por ejemplo, al ir directamente a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com/about&lt;/code&gt;, Netlify mostrará un error 404 genérico.&lt;/p&gt;

&lt;p&gt;Pero cuando vas a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com&lt;/code&gt; y haces clic en un enlace que te lleva a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com/about&lt;/code&gt;, todo funciona correctamente.&lt;/p&gt;

&lt;p&gt;¿Suena familiar?&lt;/p&gt;

&lt;h2 id=&quot;la-solución&quot;&gt;La solución&lt;/h2&gt;

&lt;p&gt;Agrega en tu carpeta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt; un archivo llamado &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_redirects&lt;/code&gt; y agrega este contenido: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/* /index.html 200&lt;/code&gt;&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/muZk/fc63e58eac1ecae7d88492557233c082.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Eso dice que todo request (el “/*”) lo redireccione a “/index.html” y que responda con código HTTP 200.&lt;/p&gt;

&lt;p&gt;Esto hace que cuando vayas a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com/about&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Netlify dice: “ah, tengo que responder con &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt; todo lo que me pregunten”. Así que envía el archivo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Tu browser recibe el archivo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt; y lo carga.&lt;/li&gt;
  &lt;li&gt;El &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt; carga tu aplicación React&lt;/li&gt;
  &lt;li&gt;Tu aplicación React carga &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;react-router&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;react-router&lt;/code&gt; ve que estás en la ruta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/about&lt;/code&gt; y hace toda su magia.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Y es así como tu sitio funciona de maravilla.&lt;/p&gt;

&lt;h2 id=&quot;por-qué-ocurre-el-problema-de-404-con-react-router-en-netlify&quot;&gt;Por qué ocurre el problema de 404 con react-router en Netlify&lt;/h2&gt;

&lt;p&gt;Recordemos que una aplicación React es una aplicación de cliente. Primero tiene que cargar el HTML, luego el JavaScript. Entonces todo lo relacionado con las rutas se maneja &lt;strong&gt;luego&lt;/strong&gt; de cargar el HTML.&lt;/p&gt;

&lt;p&gt;Netlify responde con lo que tienes en &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.html&lt;/code&gt; cuando entras a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com&lt;/code&gt;, pero no sabe qué hacer cuando entras directamente a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com/about&lt;/code&gt; (sí, es medio bruto).&lt;/p&gt;

&lt;p&gt;Seguramente, lo que intenta hacer cuando entras a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tusitio.com/about&lt;/code&gt; es buscar el archivo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;about.html&lt;/code&gt; o &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;about/index.html&lt;/code&gt; dentro de tu carpeta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public&lt;/code&gt;. Como no los encuentra: BOOM! 404.&lt;/p&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="es" /><category term="programacion" /><summary type="html">Netlify es medio bruto. Aquí la solución al problema.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2021-01-08-problema-404-netlify-react/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2021-01-08-problema-404-netlify-react/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="es"><title type="html">Integrando Mercadopago en una single page app (SPA)</title><link href="https://codewithnico.com/mercadopago-spa/" rel="alternate" type="text/html" title="Integrando Mercadopago en una single page app (SPA)" /><published>2020-10-05T19:00:00+00:00</published><updated>2020-10-05T19:00:00+00:00</updated><id>https://codewithnico.com/mercadopago-spa</id><content type="html" xml:base="https://codewithnico.com/mercadopago-spa/">&lt;blockquote&gt;
  &lt;p&gt;La documentación es pésima! – Un desarrollador al integrar MercadoPago&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Este año hice una implementación de Mercadopago con React. A pesar de haber participado en otras integraciones (Stripe, Paypal y Webpay), me encontré con varias sorpresas y trabas en el camino.&lt;/p&gt;

&lt;p&gt;Una de ellas es la documentación. Si bien no es tan mala (hay peores), carece de ejemplos. Me da la impresión de que la hicieron entre varias personas al estilo “cada uno hace su parte”.&lt;/p&gt;

&lt;p&gt;Este post es para ayudarte a entender qué es lo que tienes que hacer al integrarte con MercadoPago, para que así puedas aplicarlo a cualquier frontend (React, Angular, Vue, etc) y a cualquier backend.&lt;/p&gt;

&lt;p&gt;En este post no encontrarás un paso a paso, ni verás mucho código de ejemplo. No quiero que copies y pegues código. Quiero que entiendas qué es lo que hay que hacer, para que puedas ajustar y aplicarlo en tu contexto.&lt;/p&gt;

&lt;h2 id=&quot;la-forma-más-rápida-de-integrarse-con-mercadopago&quot;&gt;La forma más rápida de integrarse con Mercadopago&lt;/h2&gt;

&lt;p&gt;Lo primero que tienes que saber es que existen muchas formas de integrarse con MercadoPago, las cuales aparecen en su página web:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-10-14-mercado-pago-spa/opciones-integracion-mp.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Dentro de las opciones para plataformas web, la más fácil es &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/integration&quot;&gt;Checkout Pro&lt;/a&gt;. Es la más fácil porque MercadoPago te pasa una librería que incluyes en tu HTML y que tiene listo el frontend de la “pasarela de pago” (donde tu usuario realiza el pago).&lt;/p&gt;

&lt;p&gt;Si te ingregaras con Checkout API, tendrías que hacer tu mismo el frontend (como lo hace Sony Chile o PedidosYa).&lt;/p&gt;

&lt;p&gt;Y como queremos desarrollar lo más rápido nuestro producto, la opción que te recomiendo es Checkout Pro.&lt;/p&gt;

&lt;p&gt;Ahora bien, ¿qué tienes que hacer para integrarte con Checkout Pro?&lt;/p&gt;

&lt;h2 id=&quot;flujo-de-integración-con-checkout-pro-de-mercadopago&quot;&gt;Flujo de integración con Checkout Pro de MercadoPago&lt;/h2&gt;

&lt;p&gt;Antes de entrar en el flujo de integración con MercadoPago, quiero hablarte del flujo general de una aplicación web que vende cosas.&lt;/p&gt;

&lt;p&gt;El flujo general de venta en plataformas web es así:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;El usuario navega por el sitio y decide comprar un producto.&lt;/li&gt;
  &lt;li&gt;El usuario paga el producto.&lt;/li&gt;
  &lt;li&gt;El servidor recibe una orden de pago exitosa, y luego “entrega” el producto.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Esto es una simplificación, ya que como has notado:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Hay aplicaciones donde seleccionar 1 o más productos y los agregas a un “carrito de compra”&lt;/li&gt;
  &lt;li&gt;La “entrega” del producto depende mucho de lo que estés vendiendo. Si vendes productos físicos, “entregar” significa coordinar toda la entrega del producto. Si vendes un producto digital como e-book, entregar puede ser enviar el e-book por correo. Si vendes moneda virtual (como en un juego), entregar significa cargarle la moneda a la cuenta del usuario.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eso tienes que tenerlo en cuenta, porque MercadoPago te ayuda solo en el paso 2, cobrarle al usuario. Si haces carrito de compra, tú tienes que programar toda la lógica del carrito de compra.&lt;/p&gt;

&lt;p&gt;Dicho esto, el proceso de pago con &lt;em&gt;Checkout Pro&lt;/em&gt; es así:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Muestras en la página el botón de pago de MP, para lo cual tienes que insertar un &lt;em&gt;script&lt;/em&gt; en tu HTML.&lt;/li&gt;
  &lt;li&gt;Cuando el usuario pague, MP notificará a tu servidor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Suena fácil… pero no lo es. El diablo está en los detalles.&lt;/p&gt;

&lt;p&gt;A continuación te explicaré lo que tienes que hacer para lograr estas 2 cosas.&lt;/p&gt;

&lt;h2 id=&quot;parte-1-cómo-mostrar-el-botón-de-pago&quot;&gt;Parte 1: ¿cómo mostrar el botón de pago?&lt;/h2&gt;

&lt;p&gt;Para mostrar el botón de pago, debes leer la sección &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/integration&quot;&gt;“Integra Checkout Pro”&lt;/a&gt;. Todo se resume a:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Crea una &lt;em&gt;preferencia&lt;/em&gt; en MP&lt;/li&gt;
  &lt;li&gt;Pega un código en tu página HTML&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Antes de continuar… ¿qué rayos es una preferencia? En el &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/previous-requirements/&quot;&gt;Glosario&lt;/a&gt; dice que una preferencia es *la &lt;strong&gt;información del producto o servicio que se quiere ofrecer&lt;/strong&gt;. *Mi modelo mental de la preferencia es una orden de compra, que tiene información de todos los productos que estoy comprando.&lt;/p&gt;

&lt;p&gt;Ahora sí. ¿Cómo implementamos los 2 pasos en una SPA (React) con un backend propio? ¿Dónde va cada cosa?&lt;/p&gt;

&lt;p&gt;Te voy a explicar con palabras el flujo exacto que debes implementar para mostrar el botón de pago. Son 5 pasos:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Antes de querer mostrar el botón de pago (por ejemplo, al mostrar el detalle de producto), tu SPA (React, Vue, etc) debe decirle a tu backend: &lt;em&gt;“Hey, un usuario quiere comprar este producto”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Tu backend, procesa este pedido y luego le dice a MercadoPago: &lt;em&gt;“Hey, alguien quiere comprar este producto. Te mando los detalles”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;MP le responde a tu backend: &lt;em&gt;“Listo. Toma este ID, con eso tu cliente podrá pagar”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Luego tu backend le responde a tu frontend: &lt;em&gt;“Todo ok. Toma este ID para que el usuario pague”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Ahora tu frontend toma el ID, e inserta un código en la página que hará que aparezca un botón de pago.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;En otras palabras, tu frontend hace un request al backend solicitando el ID de la preferencia (requerido para mostrar el botón de pago de MP), y tu backend hace un request a MercadoPago para crear la preferencia, y así responder el request con el ID.&lt;/p&gt;

&lt;p&gt;Al comienzo dije que no mostraría código, pero si quieres inspiración:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Puedes ver un ejemplo en React del punto 1 y 5 &lt;a href=&quot;https://gist.github.com/muZk/e11931b3df6aab7c7dd6dd53058c3e41&quot;&gt;aquí&lt;/a&gt; (es parecido a lo que hice en mi proyecto)&lt;/li&gt;
  &lt;li&gt;Un ejemplo en node.js para el punto 2 al 4 &lt;a href=&quot;https://gist.github.com/muZk/717ff8ec882dd701f1bec2e9ac2c6b62&quot;&gt;aquí&lt;/a&gt; (basado en &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/integration&quot;&gt;“Genera tu preferencia”&lt;/a&gt; de la documentación de MP)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Nota: puede que el código de ejemplo contenga errores, ya que los hice a pulso sin verificarlos. Lo importante es la idea.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Con todo lo anterior ya puedes mostrar el botón de pago. Por ejemplo, en la app donde lo integré se terminó viendo así:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-10-14-mercado-pago-spa/tienda-1.png&quot; alt=&quot;Preview de la tienda&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-10-14-mercado-pago-spa/tienda-2.png&quot; alt=&quot;Preview del pago&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;parte-2-cómo-procesar-el-pago&quot;&gt;Parte 2: ¿cómo procesar el pago?&lt;/h2&gt;

&lt;p&gt;La parte anterior te dije cómo mostrar la pasarela de pago para poder recibir pagos.&lt;/p&gt;

&lt;p&gt;Como mencioné al comienzo, luego de que te paguen, querrás entregar el producto de alguna forma (sea lo que sea que “entregar” signifique en tu contexto).&lt;/p&gt;

&lt;p&gt;Para esto, MercadoPago puede notificarle a tu servidor cuando hay cambios en el estado de los pagos. Es decir, MercadoPago le dirá a tu backend &lt;em&gt;“hey!, un pibe pagó esto”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;La documentación de esto se encuentra en &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/advanced-integration&quot;&gt;“Integración Avanzada”&lt;/a&gt; (no apesta tanto como otras secciones).&lt;/p&gt;

&lt;p&gt;Lo que te dice esa página es que tienes que agregar información adicional cuando creas tu preferencia (punto 2 de la sección anterior). Debes agregar al menos lo siguiente:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Enviar el parámetro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;notification_url&lt;/code&gt; , el cual debe ser un endpoint de tu backend para recibir notificaciones de MercadoPago.&lt;/li&gt;
  &lt;li&gt;Enviar el parámetro &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;external_reference&lt;/code&gt; cuyo valor sea un ID de tu sistema que represente la orden de compra. Esto será útil para cruzar información entre tu sistema y MercadoPago.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;El código actualizado (con IPN) de ejemplo del backend lo puedes ver &lt;a href=&quot;https://gist.github.com/muZk/2453663c05e15884ac08cba39747e8ab&quot;&gt;aquí&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;Nota: El listado completo de parámetros de una preferencia lo puedes ver &lt;a href=&quot;https://www.mercadopago.cl/developers/es/reference/preferences/_checkout_preferences/post/&quot;&gt;aquí&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hasta aquí ya deberías tener una integración funcional:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Puedes mostrar el botón de pago en tu app en React gracias al &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;preferenceId&lt;/code&gt; obtenido del backend.&lt;/li&gt;
  &lt;li&gt;El backend le informó a MercadoPago el producto que está incluido en la orden, una URL de notificación para saber que pasa con el pago y además le indicó un ID que representa a la orden en tu BD.&lt;/li&gt;
  &lt;li&gt;Cuando el usuario pague, MercadoPago te enviará un request al endpoint que le dijiste. Tu backend procesará la información, verá que todo esté bien (el pago fue por el monto que debía ser, el pago fue aprobado, etc). Y finalmente ejecutará la entrega del producto.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;últimos-detalles&quot;&gt;Últimos detalles&lt;/h2&gt;

&lt;p&gt;La preferencia recibe un parámetro llamado &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;back_urls&lt;/code&gt; donde puedes indicar URL de tu &lt;strong&gt;frontend&lt;/strong&gt; para mostrar cuando el pago sea un éxito, fracaso o esté pendiente.&lt;/p&gt;

&lt;p&gt;Otra forma de redireccionar luego del pago es mediante del formulario donde inyectas el botón de pago. Por ejemplo, lo que yo hago es en el &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;action&lt;/code&gt; de ese form me redirija a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/orders/:id&lt;/code&gt; (del frontend) donde tengo más control para mostrar el estado de una orden particular. El código exacto de mi implementación en React lo puedes ver &lt;a href=&quot;https://gist.github.com/muZk/2dd0f43d483effa8f15a6070cc2ea6d3&quot;&gt;aquí&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;links-de-interés&quot;&gt;Links de interés&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Documentación de Checkout Pro: &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/integration&quot;&gt;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/integration&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Crear preferencia (MercadoPago API): &lt;a href=&quot;https://www.mercadopago.cl/developers/es/reference/preferences/_checkout_preferences/post/&quot;&gt;https://www.mercadopago.cl/developers/es/reference/preferences/_checkout_preferences/post/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Integración avanzada: &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/advanced-integration&quot;&gt;https://www.mercadopago.cl/developers/es/guides/online-payments/checkout-pro/advanced-integration&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Documentación del SDK de Node: &lt;a href=&quot;https://www.mercadopago.cl/developers/es/guides/sdks/official/nodejs/&quot;&gt;https://www.mercadopago.cl/developers/es/guides/sdks/official/nodejs/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Código de ejemplo de botón de pago en React: &lt;a href=&quot;https://gist.github.com/muZk/e11931b3df6aab7c7dd6dd53058c3e41&quot;&gt;https://gist.github.com/muZk/e11931b3df6aab7c7dd6dd53058c3e41&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Código de ejemplo de creación de preferencia en node.js: &lt;a href=&quot;https://gist.github.com/muZk/717ff8ec882dd701f1bec2e9ac2c6b62&quot;&gt;https://gist.github.com/muZk/717ff8ec882dd701f1bec2e9ac2c6b62&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Código de ejemplo de integración avanzada en node.js: &lt;a href=&quot;https://gist.github.com/muZk/2453663c05e15884ac08cba39747e8ab&quot;&gt;https://gist.github.com/muZk/2453663c05e15884ac08cba39747e8ab&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="es" /><category term="programacion" /><summary type="html">La documentación es pésima! – Un desarrollador al integrar MercadoPago</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/%7B%22width%22=%3E800,%20%22height%22=%3E422%7D" /><media:content medium="image" url="https://codewithnico.com/%7B%22width%22=%3E800,%20%22height%22=%3E422%7D" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">How to style your React application?</title><link href="https://codewithnico.com/how-to-style-your-react-application/" rel="alternate" type="text/html" title="How to style your React application?" /><published>2020-09-17T00:00:00+00:00</published><updated>2020-09-17T00:00:00+00:00</updated><id>https://codewithnico.com/how-to-style-your-react-application</id><content type="html" xml:base="https://codewithnico.com/how-to-style-your-react-application/">&lt;blockquote&gt;
  &lt;p&gt;I built my first app. It works. Now how the heck do I go about styling it?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When building a React application, you get to a point where it is fully functional but it doesn’t look like you want.&lt;/p&gt;

&lt;p&gt;We as developers focus on &lt;em&gt;how things work&lt;/em&gt;, not on &lt;em&gt;how they look&lt;/em&gt;. Until we realize that our little baby is ugly 😭 and then we start styling and resizing here and there to make it prettier.&lt;/p&gt;

&lt;p&gt;But how the heck can we style all of them in a way that is efficient and makes sense?&lt;/p&gt;

&lt;h2 id=&quot;the-fastest-way-to-style-your-react-app&quot;&gt;The fastest way to style your React app&lt;/h2&gt;

&lt;p&gt;As you may be guessing, the fastest way to style your React app is by using a &lt;em&gt;UI component library&lt;/em&gt; that do all the heavy lifting for you.&lt;/p&gt;

&lt;p&gt;What the heck is a &lt;em&gt;UI component library&lt;/em&gt;? Well, it is a set of ready-made UI components such as buttons, forms, dialogs, and so on.&lt;/p&gt;

&lt;p&gt;When using UI component libraries, instead of playing with margins, paddings and sizes, you just use the components the library provides. It makes our lives easier because:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It is faster than creating components from scratch.&lt;/li&gt;
  &lt;li&gt;Less error prone: they already work and are tested.&lt;/li&gt;
  &lt;li&gt;They look great without any extra effort from the developer.&lt;/li&gt;
  &lt;li&gt;Established libraries are easy to use and have great documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using a library will save you time and your application is going to look awesome.&lt;/p&gt;

&lt;p&gt;The next question is &lt;em&gt;What UI library should you use?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is not an &lt;em&gt;industry standard&lt;/em&gt; when choosing a UI component library, but the &lt;a href=&quot;https://maxrozen.com/guide-to-component-ui-libraries-react/&quot;&gt;commonly&lt;/a&gt; used are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://ant.design/&quot;&gt;Ant Design&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://reactstrap.github.io/&quot;&gt;Bootstrap&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/couds/react-bulma-components&quot;&gt;Bulma&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://chakra-ui.com/&quot;&gt;Chakra UI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://material-ui.com/&quot;&gt;Material UI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://semantic-ui.com/&quot;&gt;Semantic UI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://tailwindui.com/&quot;&gt;Tailwind UI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My personal choices are Bootstrap and Ant Design. Bootstrap for simple apps without many functionalities and Ant Design for the rest.&lt;/p&gt;

&lt;p&gt;Why Bootstrap? I have experience building static sites with HTML + CSS + Bootstrap. So, I can leverage my knowledge in this framework with React.&lt;/p&gt;

&lt;p&gt;Why AntD? Two reasons. The first one: &lt;strong&gt;the documentation is awesome&lt;/strong&gt;. I rarely google AntD stuff when using this framework. The second reason is that &lt;strong&gt;it has MANY components&lt;/strong&gt;. When using it, I feel like they have though in everything. Its magic.&lt;/p&gt;

&lt;p&gt;MaterialUI documentation is pretty good too. But in my experience it is a bit harder to customize and I feel like all apps made with material design looks like Google apps. Which is not bad BTW, I just don’t like it.&lt;/p&gt;

&lt;p&gt;I can’t say much about the rest because I haven’t used them. However, I know that they are safe bets and I’m personally open to experiment with them depending on the requirements of the project.&lt;/p&gt;

&lt;p&gt;My final advice is: &lt;strong&gt;choose one that looks similar to what you want to accomplish&lt;/strong&gt;. The more similar they are, the less time you spend on customization.&lt;/p&gt;

&lt;h2 id=&quot;color-personalization-how-to-choose-your-app-colors&quot;&gt;Color personalization: how to choose your App colors?&lt;/h2&gt;

&lt;p&gt;At some point in the project, you will realize that your app needs some personalized touch.&lt;/p&gt;

&lt;p&gt;The low hanging fruit in UI personalization are the colors.&lt;/p&gt;

&lt;p&gt;Most libraries have a theme system based on primary and secondary colors, you can change them easily.&lt;/p&gt;

&lt;p&gt;The tricky part is &lt;em&gt;choosing&lt;/em&gt; the colors. What colors should you use?&lt;/p&gt;

&lt;p&gt;Given that my design knowledge is zero, I usually use a palette color tool such as &lt;a href=&quot;https://colorhunt.co/&quot;&gt;colorhunt&lt;/a&gt; or &lt;a href=&quot;https://coolors.co/&quot;&gt;coolors&lt;/a&gt;. Thanks to them, you can choose any one you like with visual inspection:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-09-17-how-to-style-your-react-application/colorhunt.png&quot; alt=&quot;Colorhunt example&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Color Hunt example&lt;/figcaption&gt;

&lt;p&gt;For example, for a &lt;a href=&quot;https://impuestos.netlify.app/&quot;&gt;Chilean Tax payment calculator&lt;/a&gt; I made, the colors I used were a variation of &lt;a href=&quot;https://colorhunt.co/palette/22672&quot;&gt;this palette&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-09-17-how-to-style-your-react-application/demo.gif&quot; alt=&quot;Color Hunt in action&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Color hunt in action: using palettes, you can choose one that looks good for your project.&lt;/figcaption&gt;

&lt;p&gt;Sometimes you’re told about the dominant color. Maybe your boss told you so or maybe you feel that you should use a particular color.&lt;/p&gt;

&lt;p&gt;In either case, you already know the main color, so you can use &lt;a href=&quot;https://color.adobe.com/create/color-wheel&quot;&gt;Adobe Color Wheel&lt;/a&gt; to get complementary colors that looks nice with your choice:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-09-17-how-to-style-your-react-application/triad.png&quot; alt=&quot;Complementary colors with Adobe Color Wheel&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Looking for complementary colors with Adobe Color Wheel&lt;/figcaption&gt;

&lt;h2 id=&quot;personalization-is-not-all-about-colors&quot;&gt;Personalization is not all about colors&lt;/h2&gt;

&lt;p&gt;Sometimes you will have to make a component and styles by yourself.&lt;/p&gt;

&lt;p&gt;There are many ways to add styles to your React components. See &lt;a href=&quot;https://www.robinwieruch.de/react-css-styling&quot;&gt;How to CSS Style in React&lt;/a&gt; for a good summary of the approaches.&lt;/p&gt;

&lt;p&gt;I usually start with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inline style&lt;/code&gt; until &lt;em&gt;I feel&lt;/em&gt; it is not good. My next step is to use &lt;a href=&quot;https://cssinjs.org/?v=v10.4.0&quot;&gt;css-in-js&lt;/a&gt; with &lt;a href=&quot;https://cssinjs.org/react-jss/?v=v10.4.0&quot;&gt;react-jss&lt;/a&gt;. This approach works well because when using UI libraries, custom components tend to be self-contained.&lt;/p&gt;

&lt;p&gt;If you’re having troubles maintaining your styles, you could try to use another solution. Maybe &lt;em&gt;Utility-First-CSS&lt;/em&gt; is right for you. One of the popular approach here is &lt;a href=&quot;https://tailwindcss.com/&quot;&gt;Tailwind CSS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re having trouble distinguishing the different strategies, check &lt;a href=&quot;https://www.robinwieruch.de/react-css-styling&quot;&gt;How to CSS Style in React&lt;/a&gt; post.&lt;/p&gt;

&lt;p&gt;And lastly, when creating your own components, check &lt;a href=&quot;https://medium.com/refactoring-ui/7-practical-tips-for-cheating-at-design-40c736799886&quot;&gt;7 Practical Tips for Cheating at Design&lt;/a&gt; to avoid common design mistakes.&lt;/p&gt;

&lt;h2 id=&quot;where-to-look-for-inspiration&quot;&gt;Where to look for inspiration?&lt;/h2&gt;

&lt;p&gt;Let’s say you’re building an app that needs a chat and that your UI component library doesn’t have components for it. How do you know how it should look like? Where can you get design inspiration?&lt;/p&gt;

&lt;p&gt;If you’re like me, creating a component based on a design is the easy part. Figuring out the design is the challenging part. Especially when you don’t have a designer on your team.&lt;/p&gt;

&lt;p&gt;When I’m lost, I usually go to &lt;a href=&quot;https://dribbble.com/shots/following/web-design&quot;&gt;Dribbble&lt;/a&gt; for inspiration. Or I look at the apps I already use.&lt;/p&gt;

&lt;p&gt;For example, early in this year I was building an app with Chat functionality. The choosen UI component library was &lt;a href=&quot;https://ionicframework.com/docs/react&quot;&gt;Ionic&lt;/a&gt; because it looks good on mobile. The problem is that Ionic doesn’t have componenets for Chat.&lt;/p&gt;

&lt;p&gt;After looking for inspiration in Dribbble (&lt;a href=&quot;https://dribbble.com/shots/6261272-Mobile-Leaderboard&quot;&gt;this one&lt;/a&gt;) and on the apps I use (&lt;a href=&quot;https://www.messenger.com/&quot;&gt;Facebook Messenger&lt;/a&gt;) I ended with this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-09-17-how-to-style-your-react-application/chat.png&quot; alt=&quot;Chat example based on a dribbble design and FB messenger&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Chat example based on a dribbble design and FB messenger&lt;/figcaption&gt;

&lt;h2 id=&quot;final-notes&quot;&gt;Final notes&lt;/h2&gt;

&lt;p&gt;In summary:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The fastest way to make a good-looking React app is by using a UI library. Choosing a &lt;a href=&quot;https://maxrozen.com/guide-to-component-ui-libraries-react/&quot;&gt;popular&lt;/a&gt; one is a safe bet, so choose one based on what you are going to build.&lt;/li&gt;
  &lt;li&gt;The low-hanging fruits in personalization are colors. To choose a good set of colors, you can use &lt;a href=&quot;https://colorhunt.co/&quot;&gt;Color Hunt&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;When the UI framework is not enough, you’ll need to make a custom component from scratch. You can use &lt;a href=&quot;https://medium.com/refactoring-ui/7-practical-tips-for-cheating-at-design-40c736799886&quot;&gt;tactics&lt;/a&gt; to improve your design and you have &lt;a href=&quot;https://www.robinwieruch.de/react-css-styling&quot;&gt;many choices&lt;/a&gt; to style it. Lacking of ideas? Use &lt;a href=&quot;https://dribbble.com/shots/following/web-design&quot;&gt;Dribbble&lt;/a&gt; for inspiration.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt; just found &lt;a href=&quot;https://github.com/bradtraversy/design-resources-for-developers&quot;&gt;Design Resources for Developer&lt;/a&gt; in GitHub, looks awesome!&lt;/p&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">We as developers focus on how things work, not on how they look. Until we realize that our little baby is ugly 😭 so then we start styling and resizing here and there to make it prettier. But how the heck can we style all of them in a way that is efficient and makes sense?</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-09-17-how-to-style-your-react-application/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-09-17-how-to-style-your-react-application/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">What is useRef for? When should I use it?</title><link href="https://codewithnico.com/what-is-useref-for-when-should-i-use-it/" rel="alternate" type="text/html" title="What is useRef for? When should I use it?" /><published>2020-09-09T00:00:00+00:00</published><updated>2020-09-09T00:00:00+00:00</updated><id>https://codewithnico.com/what-is-useref-for-when-should-i-use-it</id><content type="html" xml:base="https://codewithnico.com/what-is-useref-for-when-should-i-use-it/">&lt;p&gt;If you have tried reading the docs over and over again but you still don’t understand &lt;em&gt;useRef&lt;/em&gt;, this post is for you!&lt;/p&gt;

&lt;h3 id=&quot;what-is-useref&quot;&gt;What is useRef?&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useRef&lt;/code&gt; is like a box where you can store something for later use. A number, an object, anything you want.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-09-09-what-is-useref-for-when-should-i-use-it/box.png&quot; alt=&quot;What is useRef for? When should I use it?&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;useRef&lt;/figcaption&gt;

&lt;p&gt;The &lt;em&gt;thing&lt;/em&gt; you save in this box is &lt;em&gt;preserved&lt;/em&gt; between renders, similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useState&lt;/code&gt;. In other words, &lt;em&gt;the box&lt;/em&gt; is not destroyed when your component is updated.&lt;/p&gt;

&lt;p&gt;And when you change the box content, nothing happens to the component. The box content changes, but the component remains the same.&lt;/p&gt;

&lt;p&gt;Observations:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Unlike component state variables (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useState&lt;/code&gt;), updating a ref value doesn’t trigger a new render.&lt;/li&gt;
  &lt;li&gt;Are you coming from class components? Ok, it is like storing something in an instance variable (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.value = someValue&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;what-is-it-used-for&quot;&gt;What is it used for?&lt;/h3&gt;

&lt;p&gt;Ok, cool. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useRef&lt;/code&gt; is used to store something in the component “memory” but it doesn’t trigger a new render when the value is updated.&lt;/p&gt;

&lt;p&gt;But what is it used for?&lt;/p&gt;

&lt;p&gt;Well, there are two main use cases:&lt;/p&gt;

&lt;h3 id=&quot;1-to-store-dom-elements-so-you-can-later-do-something-with-it&quot;&gt;1) &lt;strong&gt;To store DOM elements&lt;/strong&gt; so you can later do something with it.&lt;/h3&gt;

&lt;p&gt;For example, you can focus the password input when the user press “ENTER” on the email input:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://codewithnico.com/content/images/2020/09/no-click.gif&quot; alt=&quot;What is useRef for? When should I use it?&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Focus the next element when pressing &quot;ENTER&quot;&lt;/figcaption&gt;

&lt;p&gt;You can do it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useRef&lt;/code&gt; (&lt;a href=&quot;https://codesandbox.io/s/nostalgic-torvalds-irewu?file=/src/App.js&quot;&gt;source code&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;emailRef&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;passwordRef&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;htmlFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;email&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Email:&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;input&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;email&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;emailRef&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;onKeyPress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;charCode&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
              &lt;span class=&quot;nx&quot;&gt;passwordRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;focus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;label&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;htmlFor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;password&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Password:&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;password&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;passwordRef&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;useRef example to store reference to an input component&lt;/figcaption&gt;

&lt;h3 id=&quot;2-to-store-values-you-want-to-keep-an-eye-on&quot;&gt;2) To store values you want to keep an eye on&lt;/h3&gt;

&lt;p&gt;Sometimes you want to keep an eye on a value but you don’t need to trigger new renders when it changes.&lt;/p&gt;

&lt;p&gt;Example: &lt;a href=&quot;https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state&quot;&gt;storing the previous value of a state variable&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is a simple application that display a counter and its previous values. It also has two buttons to increase the counter (by 1 and by 10):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://codewithnico.com/content/images/2020/09/Sep-09-2020-17-16-14.gif&quot; alt=&quot;What is useRef for? When should I use it?&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Example of useRef to store a previous value&lt;/figcaption&gt;

&lt;p&gt;&lt;a href=&quot;https://codesandbox.io/s/previous-value-with-useref-doh5p&quot;&gt;Source code&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prevCountRef&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;prevCountRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;current&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prevCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;prevCountRef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;current&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Previous value with useRef&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
          Increase counter by 1
        &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;onClick&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
          Increase counter by 10
        &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;button&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        Now: &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;, before: &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prevCount&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;useRef to store a previous value&lt;/figcaption&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">If you have tried reading the docs over and over again but you still don't understand useRef, this post is for you!</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-09-09-what-is-useref-for-when-should-i-use-it/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-09-09-what-is-useref-for-when-should-i-use-it/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">From class component to hooks (Cheatsheet included)</title><link href="https://codewithnico.com/from-class-components-to-hooks/" rel="alternate" type="text/html" title="From class component to hooks (Cheatsheet included)" /><published>2020-08-03T00:00:00+00:00</published><updated>2020-08-03T00:00:00+00:00</updated><id>https://codewithnico.com/from-class-components-to-hooks</id><content type="html" xml:base="https://codewithnico.com/from-class-components-to-hooks/">&lt;p&gt;You are comfortable with class components and you are wondering if you should learn React hooks (short answer: &lt;em&gt;yes, you should&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Don’t worry about losing your job if you don’t learn hooks because class components &lt;a href=&quot;https://reactjs.org/docs/hooks-faq.html#do-i-need-to-rewrite-all-my-class-components&quot;&gt;won’t be deprecated&lt;/a&gt; and there is no need to update all codebases to hooks.&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;hooks are a thing&lt;/strong&gt;. They may look over-hyped, but they are important specially when starting something from scratch.&lt;/p&gt;

&lt;p&gt;I know that feeling when you’re comfortable with a technology and you’ve to learn a new toy (I had to learn Node when I was used to Ruby on Rails 😭)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-08-03-from-class-components-to-hooks/ah-shit.png&quot; alt=&quot;Ah shit, here we go again&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;You deciding to learn hooks&lt;/figcaption&gt;

&lt;p&gt;In this post we are going to use what you already know to kick-off your hooks journey. I know that your brain works well with classes, so lets take this advantage!&lt;/p&gt;

&lt;h2 id=&quot;refactoring-a-simple-component&quot;&gt;Refactoring a simple component&lt;/h2&gt;

&lt;p&gt;One of the first things we learn in React (classes) is the &lt;a href=&quot;https://reactjs.org/docs/react-component.html#the-component-lifecycle&quot;&gt;component lifecycle&lt;/a&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentDidMount&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentWillUnmoun&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s use them and create a &lt;a href=&quot;https://1toqw.csb.app/&quot;&gt;“you have been X seconds here”&lt;/a&gt; example.&lt;/p&gt;

&lt;p&gt;This app will show how long have you been in the page. To build this, we will:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;When the component is initialized, we initialize a state with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;counter = 0&lt;/code&gt;. We do this in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;After the component is mounted, we start a timer interval that increases the counter by one every 1000 milliseconds (1 second). We do this in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentDidMount&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Before the component is unmounted, we clear the timeout. We do this in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentWillUnmount&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is the implementation of this app:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;componentDidMount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;intervalID&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}));&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;componentWillUnmount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;intervalID&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;
  &lt;a href=&quot;https://codesandbox.io/s/async-shadow-1toqw?file=/src/App.js&quot; target=&quot;_blank&quot;&gt;Demo&lt;/a&gt;
&lt;/figcaption&gt;

&lt;p&gt;I hope you don’t see anything strange here. It uses class component with the classic component lifecycle methods.&lt;/p&gt;

&lt;p&gt;Now I’ll show you how to refactor it to hooks.&lt;/p&gt;

&lt;h2 id=&quot;step-1-transform-your-class-component-into-a-function-component&quot;&gt;Step 1: transform your class component into a function component&lt;/h2&gt;

&lt;p&gt;In other words:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;function&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Remove &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;extends React.Component&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Remove everything inside except the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt; code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In our case:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is not working yet. We have to modify it a bit…&lt;/p&gt;

&lt;h2 id=&quot;step-2-state-management-with-usestate&quot;&gt;Step 2: state management with useState&lt;/h2&gt;

&lt;p&gt;You know how to use state in class components:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Initialize &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Update it using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setState&lt;/code&gt; method which triggers a new render.&lt;/li&gt;
  &lt;li&gt;Read state properties from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.state&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In functional components, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; works similar:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;it keeps values between renders&lt;/li&gt;
  &lt;li&gt;updating it triggers a new render&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, in function components, you don’t have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constructor&lt;/code&gt; and you don’t have instance methods such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the mechanics here are a bit different. To use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; in a functional component, you have to use a &lt;strong&gt;hook&lt;/strong&gt;, which is a function that gives superpowers to your functional components.&lt;/p&gt;

&lt;p&gt;The hook that brings &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; superpower to our function components is called &lt;a href=&quot;https://reactjs.org/docs/hooks-state.html&quot;&gt;useState hook&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;FunctionalComponentWithState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;someState&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;initialValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;-- state superpower!!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Using useState hook&lt;/figcaption&gt;

&lt;p&gt;By calling &lt;em&gt;useState&lt;/em&gt;, &lt;a href=&quot;https://daveceddia.com/usestate-hook-examples/&quot;&gt;you are creating a single piece of state&lt;/a&gt; associated to the component. It is similar to initializing the state in the constructor of a class component.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useState&lt;/code&gt; returns two things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;the current value of the state variable&lt;/li&gt;
  &lt;li&gt;a function to update it:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ComponentWithState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;currentValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;updateValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;initialValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Creating a piece of state, getting the current value and a function to update it. Magic! 🤯&lt;/figcaption&gt;

&lt;blockquote&gt;
  &lt;p&gt;Well, technically &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useState&lt;/code&gt; returns an array of size two where the first element is the current value and the second element is a function to update it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets go back to the example. We need to store the counter, so instead of&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Reading state in a class component&lt;/figcaption&gt;

&lt;p&gt;We should use:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 0 is the initial value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;In one line we tell react to create a state variable with an initial value, get the current value and a function to update it.&lt;/figcaption&gt;

&lt;p&gt;Now our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;App&lt;/code&gt; code should look like this:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;Functional component with state&lt;/figcaption&gt;

&lt;p&gt;&lt;a href=&quot;https://codesandbox.io/s/weathered-sun-fdgeq&quot;&gt;This works!&lt;/a&gt; But we are not done yet because we are not counting every second.&lt;/p&gt;

&lt;h2 id=&quot;step-3-implement-componentdidmount-with-useeffect-hook&quot;&gt;Step 3: implement componentDidMount with useEffect hook&lt;/h2&gt;

&lt;p&gt;In function components, to execute code after the component has been rendered you have to use another superpower (hook) called &lt;a href=&quot;https://reactjs.org/docs/hooks-effect.html&quot;&gt;useEffect hook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt; hook takes a function which will be invoked after render. Very similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentDidMount&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// &quot;componentDidMount&quot; logic should be here&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;Adding &lt;code&gt;componentDidMount&lt;/code&gt; logic&lt;/figcaption&gt;

&lt;p&gt;After &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt; (in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt;) we have to:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;start the timer (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setTimeout&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;update the counter on “every tick”.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setState&lt;/code&gt; we should use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setCounter&lt;/code&gt;. The state update logic:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Code to update state in class components&lt;/figcaption&gt;

&lt;p&gt;is replaced with:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Update state with hooks&lt;/figcaption&gt;

&lt;p&gt;When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setCounter&lt;/code&gt; is called, &lt;strong&gt;it will trigger a new render&lt;/strong&gt;, similar to calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.setState&lt;/code&gt; in the class component.&lt;/p&gt;

&lt;p&gt;Now our App looks like this:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which does almost everything!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I said before that the &lt;em&gt;function you pass to useEffect is invoked after render&lt;/em&gt;. That is different than &lt;em&gt;after mount&lt;/em&gt;. &lt;strong&gt;The empty array is used to tell React to invoke only once&lt;/strong&gt; (on mount!)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;--- HERE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;useEffect that runs only once&lt;/figcaption&gt;

&lt;h2 id=&quot;step-4-implement-componentwillunmount-with-useeffects-cleanup-function&quot;&gt;Step 4: implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentWillUnmount&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt;’s cleanup function&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt; can return a function that &lt;a href=&quot;https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup&quot;&gt;cleans up effects&lt;/a&gt; from the previous render, which can be used to remove subscriptions (for example, to call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;clearInterval&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;If your effect runs only on &lt;em&gt;“mount”&lt;/em&gt; (like our component), the cleanup logic will run only on &lt;em&gt;unmount&lt;/em&gt;. Our updated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;cont&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;intervalId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;--- returns a &quot;cleanup&quot; function&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// &quot;on unmount&quot;&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;intervalId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;useEffect with &quot;on unmount&quot; logic&lt;/figcaption&gt;

&lt;h2 id=&quot;the-final-code&quot;&gt;The final code&lt;/h2&gt;

&lt;p&gt;And the final code:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  
  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;intervalId&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;setCounter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;oldCounterValue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;clearInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;intervalId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
  
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;textAlign&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;center&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Hey&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;You have been there for &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;counter&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; seconds.&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;
  See in &lt;a href=&quot;https://codesandbox.io/s/weathered-sun-fdgeq&quot; target=&quot;_blank&quot;&gt;codesandbox&lt;/a&gt;
&lt;/figcaption&gt;

&lt;h2 id=&quot;recap&quot;&gt;Recap&lt;/h2&gt;

&lt;h3 id=&quot;usestate-instead-of-thisstate----&quot;&gt;&lt;em&gt;useState&lt;/em&gt; instead of &lt;em&gt;this.state = { … }&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useState&lt;/code&gt; asks for an initial value, and returns the current value and a function to update it. All in a single line of code:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;currentValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;initialValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Component state management with useState hook&lt;/figcaption&gt;

&lt;p&gt;Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setValue&lt;/code&gt; will trigger a new render, just like using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;this.setState&lt;/code&gt;.&lt;/p&gt;

&lt;h3 id=&quot;useeffect-instead-of-componentdidmount--componentwillunmount&quot;&gt;&lt;em&gt;useEffect&lt;/em&gt; instead of &lt;em&gt;componentDidMount&lt;/em&gt; / &lt;em&gt;componentWillUnmount&lt;/em&gt;&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useEffect&lt;/code&gt; takes a function which is invoked after render. This is generally used to run code with side effects such as HTTP requests, subscriptions, etc. Similar to what you do in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;componentDidMount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can tell react to run &lt;em&gt;cleanup logic&lt;/em&gt; (removing a subscription for example) that you added in your effect by making your effect return a function.&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// on mount: do something&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// on unmount: do something&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// &amp;lt;--- empty array = tell react to run it once&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;&lt;code&gt;useEffect&lt;/code&gt; that imitates &lt;code&gt;componentDidMount&lt;/code&gt; and &lt;code&gt;componentWillUnmount&lt;/code&gt;&lt;/figcaption&gt;

&lt;hr /&gt;

&lt;p&gt;Too much to remember? Don’t worry, I made a &lt;em&gt;Class components to Hooks&lt;/em&gt; Cheatsheet you can use 🎁 💝&lt;/p&gt;

&lt;p&gt;Get it below 👇&lt;/p&gt;

&lt;script async=&quot;&quot; data-uid=&quot;5a5d4749ec&quot; src=&quot;https://codewithnico.ck.page/5a5d4749ec/index.js&quot;&gt;&lt;/script&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">Refactoring a small class component to hooks</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-08-03-from-class-components-to-hooks/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-08-03-from-class-components-to-hooks/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">How to manage snake case for backend (Django, Rails) and camel case for React?</title><link href="https://codewithnico.com/how-to-deal-with-cases-mismatch-between-django-and-react/" rel="alternate" type="text/html" title="How to manage snake case for backend (Django, Rails) and camel case for React?" /><published>2020-07-28T00:00:00+00:00</published><updated>2020-07-28T00:00:00+00:00</updated><id>https://codewithnico.com/how-to-deal-with-cases-mismatch-between-django-and-react</id><content type="html" xml:base="https://codewithnico.com/how-to-deal-with-cases-mismatch-between-django-and-react/">&lt;p&gt;Let say that your backend framework is Django or Rails. The data you get and send must be in snake_case because this is the convention in these frameworks.&lt;/p&gt;

&lt;p&gt;But when handling this data in your React App, you’ll be using JavaScript where the convention is camelCase…&lt;/p&gt;

&lt;p&gt;How can you solve this without making your Linter go mad? Should you break the convention for either Rails or React?&lt;/p&gt;

&lt;p&gt;If you don’t mind breaking name conventions, go ahead and use snake case in your React App (sorry Linter! 💔)&lt;/p&gt;

&lt;p&gt;For the rest of us, I have a solution:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;after&lt;/em&gt; retrieving data from your backend, transform it to camelCase&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;before&lt;/em&gt; sending data to your backend, transform it to snake_case&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why in client code you might ask? Well, because 99% of the time you will be adding somekind of &lt;em&gt;fetch&lt;/em&gt; wrapper in your app (for example, to transform the responses to JSON). Just extend it a little.&lt;/p&gt;

&lt;h2 id=&quot;show-me-the-code&quot;&gt;Show me the code&lt;/h2&gt;

&lt;p&gt;The transformation is easy because there are packages for it. I’ll be using &lt;a href=&quot;https://www.npmjs.com/package/humps&quot;&gt;humps&lt;/a&gt;, which is a Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;after&lt;/em&gt; retrieving data from your backend, transform it to camelCase&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically:&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// api.js&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;humps&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;humps&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;humps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;camelizeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;GET wrapper&lt;/figcaption&gt;

&lt;blockquote&gt;
  &lt;p&gt;before sending data to your backend, transform it to snakeCase&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;// api.js&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;humps&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;humps&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stringify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;humps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;decamelizeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fetch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;POST&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;humps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;camelizeKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;POST wrapper&lt;/figcaption&gt;

&lt;p&gt;Then in your React components use your new functions and you’re done!&lt;/p&gt;

&lt;h3 id=&quot;example&quot;&gt;Example&lt;/h3&gt;

&lt;p&gt;In this example, I’ll show how to use the wrappers to display a list of pokemons using &lt;a href=&quot;https://pokeapi.co/docs/v2&quot;&gt;PokeApi&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-07-28-how-to-deal-with-cases-mismatch-between-django-and-react/demo.png&quot; alt=&quot;Demo&quot; /&gt;&lt;/p&gt;

&lt;figcaption&gt;Source code can be found &lt;a href=&quot;https://codesandbox.io/s/distracted-wilbur-zr914?file=/src/App.js&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/figcaption&gt;

&lt;p&gt;Code:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;React&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;react&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;./styles.css&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;./api&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;PokemonCard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setPokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;/pokemon/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setPokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;src&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sprites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;frontDefault&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// camelCase :D&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;margin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;96&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;96&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;display&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;inline-block&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;na&quot;&gt;backgroundImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;url(&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;`&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setPokemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]);&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/pokemon?limit=150&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setPokemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Pokemons!&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(({&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PokemonCard&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figcaption&gt;Example App using our API wrapper&lt;/figcaption&gt;

&lt;h2 id=&quot;why-it-works&quot;&gt;Why it works&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Most React apps needs some-kind of wrapper for requests. It could be to transform all requests to JSON, to add authentication tokens, etc. So extending the wrapper a little for transformations is okay and straightforward.&lt;/li&gt;
  &lt;li&gt;Sometimes you won’t be able to touch your backend code. In this case, any transformation should be done in client (React) anyway.&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">Rails and Django uses snake_case while JavaScript (React) uses camelCase. In this post, we use a simple solution to deal with it.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-07-28-how-to-deal-with-cases-mismatch-between-django-and-react/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-07-28-how-to-deal-with-cases-mismatch-between-django-and-react/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">List of Open Source React projects on GitHub to learn from</title><link href="https://codewithnico.com/production-ready-react-apps/" rel="alternate" type="text/html" title="List of Open Source React projects on GitHub to learn from" /><published>2020-05-10T00:00:00+00:00</published><updated>2020-05-10T00:00:00+00:00</updated><id>https://codewithnico.com/production-ready-react-apps</id><content type="html" xml:base="https://codewithnico.com/production-ready-react-apps/">&lt;p&gt;As you grow your knowledge in React.js, you will notice that there are many sources with simple code examples but only a few of advanced stuff. And many of them have “outdated code” (no hooks, class components everywhere, etc).&lt;/p&gt;

&lt;p&gt;What if you want to know how to structure a medium to large codebase? Or if you want to know if you are doing everything fine?&lt;/p&gt;

&lt;p&gt;One way to improve your skills is to study what other devs are doing. How they approach their projects can teach you lessons that maybe will take you weeks to learn.&lt;/p&gt;

&lt;p&gt;That’s why I made this list of open-source React.js applications you can learn from. All uses modern features such hooks, context and many uses redux-toolkit. So, no matter if you already have hit a plateau as seasoned developer or if you’re just starting, take a look!&lt;/p&gt;

&lt;h2 id=&quot;1-take-note--live--repo&quot;&gt;1) Take Note – &lt;a href=&quot;https://takenote.dev/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/taniarascia/takenote&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Take Note is an open-source project to take notes, known as &lt;em&gt;The Note Taking App for Developers&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/takenote.png&quot; alt=&quot;Take note preview&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s a simple plain-text take note app with markdown support. Without all the fancy stuff that we don’t need :)&lt;/p&gt;

&lt;p&gt;The code seems to be good organized:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/takenote-code-structure.png&quot; alt=&quot;TakeNote code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It uses React hooks (yay!) and &lt;a href=&quot;https://redux-toolkit.js.org/https://redux-toolkit.js.org/https://redux-toolkit.js.org/https://redux-toolkit.js.org/&quot;&gt;redux-toolkit&lt;/a&gt; =) (see the &lt;em&gt;slices&lt;/em&gt; folder). See how &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;slices/auth.ts&lt;/code&gt; looks like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/take-note-auth-slice.png&quot; alt=&quot;auth.ts, authentication slice using redux toolkit&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-prologapp--live--repo&quot;&gt;2) PrologApp – &lt;a href=&quot;https://prolog.profy.dev/dashboard&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/profydev/prolog-app/&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;The application is an error logging and monitoring tool similar to Sentry or Rollbar, built by &lt;a href=&quot;https://twitter.com/j_kettmann&quot;&gt;Johannes Kettmann&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/prolog-screenshot.png&quot; alt=&quot;Prolog Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It’s built with &lt;a href=&quot;https://nextjs.org/&quot;&gt;Next.js&lt;/a&gt;, a feature-based project structure, and lots of tooling like &lt;a href=&quot;https://eslint.org/&quot;&gt;ESLint&lt;/a&gt;, &lt;a href=&quot;https://prettier.io/&quot;&gt;Prettier&lt;/a&gt;, &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;TypeScript&lt;/a&gt;, &lt;a href=&quot;https://www.cypress.io/&quot;&gt;Cypress&lt;/a&gt;, and &lt;a href=&quot;https://storybook.js.org/&quot;&gt;Storybook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What do I mean by “feature-based project structure”? Well, it has a “feature” folder where you can find a “issues” and “projects” folder:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/prolog-folder-structure.png&quot; alt=&quot;Prolog folder structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And inside each folder, you can find the components, api, and types:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/prolog-feature-folder.png&quot; alt=&quot;Prolog feature folder structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you want a Codebase Tour, you can see it &lt;a href=&quot;https://www.youtube.com/watch?v=a_Z0yUsChlY&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;3-cloud-music---repo&quot;&gt;3) Cloud Music - &lt;a href=&quot;https://github.com/sanyuan0704/react-cloud-music&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;This is a clone of &lt;em&gt;NetEase&lt;/em&gt;, a cloud music streaming service. The README is in Chinese but the code is in plain English.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-demo.gif&quot; alt=&quot;Demo, GIF from README&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This project uses redux (without redux-toolkit) and hooks. What I find interesting in this project is the code structure:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-code-structure.png&quot; alt=&quot;react-cloud music code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;At first, it seems pretty standard but after reviewing the reducer (&lt;em&gt;store/reducer.js&lt;/em&gt;) I noticed that they are “grouping” (inside &lt;em&gt;application&lt;/em&gt; folder) some components and styles alongside the &lt;em&gt;redux&lt;/em&gt; logic:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-by-feature.png&quot; alt=&quot;Example of grouping files by feature&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;4-todoist-clone---repo&quot;&gt;4) Todoist clone - &lt;a href=&quot;https://github.com/karlhadwen/todoist&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/channel/UC1DUQiZduv_yNZy0O7n_iHA&quot;&gt;Karl Hadwen&lt;/a&gt; did this &lt;a href=&quot;https://todoist.com/es&quot;&gt;Todoist&lt;/a&gt; clone, and he even has a &lt;a href=&quot;https://www.youtube.com/watch?v=HgfA4W_VjmI&quot;&gt;video tutorial&lt;/a&gt; on Youtube.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/todoist-demo.png&quot; alt=&quot;Screenshot from README.me&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For the &lt;em&gt;“backend”&lt;/em&gt;, he used &lt;a href=&quot;https://firebase.google.com/&quot;&gt;Firebase&lt;/a&gt; instead of building an API from scratch. He used context and didn’t use redux (yes, you don’t always need to use redux).&lt;/p&gt;

&lt;h2 id=&quot;5-mortgage--live--repo&quot;&gt;5) Mortgage – &lt;a href=&quot;https://paulhoughton.github.io/mortgage/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/paulhoughton/mortgage&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Mortgage&lt;/em&gt; is a mortgage overpayment calculator!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/mortgage-demo.png&quot; alt=&quot;Screenshot from Live website&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It uses &lt;a href=&quot;https://d3js.org/&quot;&gt;D3&lt;/a&gt; to draw the chart and hooks! It is a small app but it is a good start to learn how to use hooks in calculator-like applications.&lt;/p&gt;

&lt;h2 id=&quot;6-tomato-work--live--repo&quot;&gt;6) Tomato Work – &lt;a href=&quot;https://tomato-work.xiejiahe.com/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/xjh22222228/tomato-work&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Tomato-work is a personal affairs management system written in React using hooks and redux:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/tomato-work-demo.gif&quot; alt=&quot;Live demo, took from README&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For the UI, it uses &lt;a href=&quot;https://ant.design/&quot;&gt;Antd&lt;/a&gt;, which is pretty awesome BTW. The code structure is standard:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/tomato-work-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What I find interesting is that it has a &lt;em&gt;services&lt;/em&gt; folder (remind me of angular) where each file is an abstraction to make API calls. So instead of calling axios directly in the component, he uses a service function instead.&lt;/p&gt;

&lt;h2 id=&quot;7-write-with-me--live--repo&quot;&gt;7) Write with me – &lt;a href=&quot;https://www.writewithme.dev&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/dabit3/write-with-me&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Write with me&lt;/em&gt; is a real-time collaborative markdown editor written in React with hooks and it uses &lt;a href=&quot;https://aws.amazon.com/es/amplify/&quot;&gt;AWS Amplify&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/write-with-me-demo.gif&quot; alt=&quot;Demo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see, this is a small project:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/write-with-me-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So, the code structure is not organized using different folders. All the state management is done using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;useReducer&lt;/code&gt; hooks.&lt;/p&gt;

&lt;p&gt;There is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;graphql&lt;/code&gt; folder because Amplify works with graphql&lt;/p&gt;

&lt;h2 id=&quot;8-jira-clone--live--repo&quot;&gt;8) JIRA Clone – &lt;a href=&quot;https://jira.ivorreic.com/project/board&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/oldboyxx/jira_clone&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;This is a simplified JIRA Clone, looks awesome!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/jira-clone-demo.png&quot; alt=&quot;Screenshot from the live website&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Analyzing the code structure, you can see it uses cypress and jest for testing:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/jira-clone-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As the author says, it is a good example of a modern real-world React codebase. There are the features (from README):&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/jira-clone-features.png&quot; alt=&quot;List of features&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;9-spectrum--live--repo&quot;&gt;9) Spectrum – &lt;a href=&quot;https://spectrum.chat/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/withspectrum/spectrum&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Spectrum is a platform that allows you to create and participate in awesome communities.&lt;/p&gt;

&lt;p&gt;I have seen many people recommending this repository. And there is a good reason for this: &lt;strong&gt;the repository is their entire product!&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“That being said, this codebase isn’t your typical open source project because it’s not a library or package with a limited scope—it’s our entire product.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In my opinion, this is one of the best open source full-stack react projects that shows off how to properly structure a large codebase&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/spectrum-code-structure.png&quot; alt=&quot;Full-Stack JS in action&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The frontend SPA (react) lives in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt; folder&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/spectrum-code-structure-spa.png&quot; alt=&quot;List of Open Source React projects on GitHub to learn from&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can infer from the folder structure, they split the code by “type” of code (components, reducers, hooks, etc).&lt;/p&gt;

&lt;h2 id=&quot;10-github-profile-readme-generator--live--repo&quot;&gt;10) GitHub profile README Generator – &lt;a href=&quot;https://rahuldkjain.github.io/gh-profile-readme-generator/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/rahuldkjain/github-profile-readme-generator&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;This tool provides an easy way to create GitHub profile README with the latest add-ons like visitors count, GitHub stats etc.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/github-profile-creator-demo.gif&quot; alt=&quot;Project Demo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is an example of a &lt;a href=&quot;https://www.gatsbyjs.com/&quot;&gt;Gatsby&lt;/a&gt; project that uses hooks. The files are organized by type which is fine for the size of the project:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/github-profile-generator-code-structure.png&quot; alt=&quot;Code organization&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you haven’t created your GitHub profile, maybe you can now by using this tool (&lt;a href=&quot;https://github.com/muzk/&quot;&gt;I did!&lt;/a&gt;)&lt;/p&gt;

&lt;h2 id=&quot;11-moose---live--repo&quot;&gt;11) Moose - &lt;a href=&quot;https://getmoose.in/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/ritz078/moose&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Moose is an application to stream, cast and download torrents built with &lt;a href=&quot;https://github.com/saltyshiomix/nextron&quot;&gt;Nextron&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-demo.png&quot; alt=&quot;Moose Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What is Nextron? It’s “Next.js” (for Server Side Rendering) + “Electron” (to built desktop app using web technologies).&lt;/p&gt;

&lt;p&gt;I don’t have experience building this kind of apps. But as far I can see, the file structure is by &lt;em&gt;file type&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-files.png&quot; alt=&quot;Moose file structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The project uses &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;TypeScript&lt;/a&gt; and for custom styles &lt;a href=&quot;https://github.com/css-modules/css-modules&quot;&gt;css-modules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-component-folder.png&quot; alt=&quot;Moose file structure&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;12-courselit---live--repo&quot;&gt;12) Courselit - &lt;a href=&quot;https://courselit.app/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/codelitdev/courselit&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Courselit is an app that helps you to start your own online teaching business.&lt;/p&gt;

&lt;p&gt;How? Well, it is a CMS (content management system) for starting creating your own courses which uses Stripe to receive payments. It is an alternative to paid solutions (like Teachable).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/images/2020-05-10-production-ready-react-apps/courselit-preview.png&quot; alt=&quot;Courselit preview&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Frontend is built with &lt;a href=&quot;https://preactjs.com/&quot;&gt;Preact&lt;/a&gt; (instead of React) with &lt;a href=&quot;https://nextjs.org/&quot;&gt;Next&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/courselit-files.png&quot; alt=&quot;Courselit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Backend is &lt;a href=&quot;https://expressjs.com/&quot;&gt;Express&lt;/a&gt; with &lt;a href=&quot;https://www.mongodb.com/&quot;&gt;MongoDB&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Something interesting is that this is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Monorepo&quot;&gt;MonoRepo&lt;/a&gt;, which means in the same github repo lives frontend and backend as separated packages (see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;packages&lt;/code&gt;). To manage multiple javascript packages in the same repository, it uses &lt;a href=&quot;https://github.com/lerna/lerna&quot;&gt;Lerna&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;13-fireact---repo---live&quot;&gt;13) Fireact - &lt;a href=&quot;https://github.com/chaoming/fireact&quot;&gt;Repo&lt;/a&gt; - &lt;a href=&quot;https://fireact-e1bdc.firebaseapp.com/&quot;&gt;Live&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Fireact is an app to build your own &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_as_a_service&quot;&gt;SaaS&lt;/a&gt; where you can receive payments through &lt;a href=&quot;https://stripe.com/es-us&quot;&gt;Stripe&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is a full-stack app built with &lt;a href=&quot;https://firebase.google.com/&quot;&gt;Firebase&lt;/a&gt; and React (&lt;a href=&quot;https://github.com/facebook/create-react-app&quot;&gt;CRA&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;File structure is a mix betweeen “file types”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/fireact-files.png&quot; alt=&quot;Fireact code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And “by page”. Pages are split between “auth pages” and “without auth”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/fireact-pages.png&quot; alt=&quot;Fireact page separation&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It uses &lt;a href=&quot;https://firebase.google.com/docs/firestore&quot;&gt;Firestore&lt;/a&gt; as you can see in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserList&lt;/code&gt; (&lt;a href=&quot;https://github.com/chaoming/fireact/blob/master/src/pages/auth/accounts/UserList/index.js#L39&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;And for the UI, it uses &lt;a href=&quot;https://coreui.io&quot;&gt;coreui&lt;/a&gt; which is an Admin template for Bootstrap:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/images/2020-05-10-production-ready-react-apps/fireact-screenshot.png&quot; alt=&quot;Fireact Screenshot&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;did-i-miss-a-project&quot;&gt;Did I miss a project?&lt;/h2&gt;

&lt;p&gt;It was a bit hard to find open-source projects using the latest react tools (e.g. hooks) 😓.&lt;/p&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">A list of modern ReactJS apps you can study!</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-05-10-production-ready-react-apps/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-05-10-production-ready-react-apps/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="es"><title type="html">Listado de apps React en Github para estudiar</title><link href="https://codewithnico.com/apps-react-para-aprender/" rel="alternate" type="text/html" title="Listado de apps React en Github para estudiar" /><published>2020-05-10T00:00:00+00:00</published><updated>2020-11-27T17:35:03+00:00</updated><id>https://codewithnico.com/apps-react-para-aprender</id><content type="html" xml:base="https://codewithnico.com/apps-react-para-aprender/">&lt;p&gt;Una de las cosas más difícil de React es que todo es muy libre, tienes muchas opciones… ¿qué utilizar para request? ¿usar hooks o usar clases? ¿qué estructura de código usar?&lt;/p&gt;

&lt;p&gt;La respuesta a cada una de esas preguntas es que depende del proyecto (🙄) pero para que te hagas una idea de todas las opciones disponibles, he hecho esta lista con proyectos construidos con React en GitHub.&lt;/p&gt;

&lt;p&gt;De esa forma, podrás estudiar el código y ver cómo juegan entre sí las distintas opciones que los autores escogieron.&lt;/p&gt;

&lt;h2 id=&quot;1-take-note--live--repo&quot;&gt;1) Take Note – &lt;a href=&quot;https://takenote.dev/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/taniarascia/takenote&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;“Take Note” es un proyecto open source para tomar notas, conocido como “la app para tomar notas para desarrolladores”&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/takenote.png&quot; alt=&quot;Take note preview&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Es una app para tomar notas en texto plano con soporte para markdown, sin editores complejos que no necesitamos :)&lt;/p&gt;

&lt;p&gt;El código se ve bastante ordenado:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/takenote-code-structure.png&quot; alt=&quot;TakeNote code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Esta aplicación usa React hooks y &lt;a href=&quot;https://redux-toolkit.js.org/https://redux-toolkit.js.org/https://redux-toolkit.js.org/https://redux-toolkit.js.org/&quot;&gt;redux-toolkit&lt;/a&gt; =) (mira la carpeta &lt;em&gt;slices&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/take-note-auth-slice.png&quot; alt=&quot;auth.ts, authentication slice using redux toolkit&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;2-cloud-music---repo&quot;&gt;2) Cloud Music - &lt;a href=&quot;https://github.com/sanyuan0704/react-cloud-music&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Es un clon de &lt;em&gt;NetEase&lt;/em&gt;, un servicio de streamming de música. El README está en Chino pero el código está en inglés.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-demo.gif&quot; alt=&quot;Demo, GIF from README&quot; /&gt;&lt;/p&gt;

&lt;p&gt;El proyecto usa redux (sin redux-toolkit) y hooks. Lo que me llama la atención es la estructura del código:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-code-structure.png&quot; alt=&quot;react-cloud music code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Se ve muy estandard, pero si miras el reducer (&lt;em&gt;store/reducer.js&lt;/em&gt;) puedes notar que están “agrupando” (dentro de la carpeta &lt;em&gt;application&lt;/em&gt;) algunos componentes junto a la lógica de redux:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/cloud-music-by-feature.png&quot; alt=&quot;Example of grouping files by feature&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;todoist-clone&quot;&gt;Todoist clone&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/channel/UC1DUQiZduv_yNZy0O7n_iHA&quot;&gt;Karl Hadwen&lt;/a&gt; creó un clon de &lt;a href=&quot;https://todoist.com/es&quot;&gt;Todoist&lt;/a&gt; e incluso tiene un &lt;a href=&quot;https://www.youtube.com/watch?v=HgfA4W_VjmI&quot;&gt;video tutorial&lt;/a&gt; en Youtube de cómo hacerlo.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/todoist-demo.png&quot; alt=&quot;Screenshot from README.me&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Para el &lt;em&gt;“backend”&lt;/em&gt;, usó &lt;a href=&quot;https://firebase.google.com/&quot;&gt;Firebase&lt;/a&gt; en vez de construir una API desde 0. En vez de usar redux u otra librería, simplemente usó Context (sí, no siempre necesitas redux).&lt;/p&gt;

&lt;h2 id=&quot;4-mortgage--live--repo&quot;&gt;4) Mortgage – &lt;a href=&quot;https://paulhoughton.github.io/mortgage/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/paulhoughton/mortgage&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Mortgage&lt;/em&gt; es una calculadora para saber cuánto tienes que pagar de hipoteca&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/mortgage-demo.png&quot; alt=&quot;Screenshot from Live website&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Usa &lt;a href=&quot;https://d3js.org/&quot;&gt;D3&lt;/a&gt; para dibujar los gráficos y usa hooks. Es una aplicación pequeña pero sirve como punto de partida para saber cómo usar hooks en este tipo de apps.&lt;/p&gt;

&lt;h2 id=&quot;5-tomato-work--live--repo&quot;&gt;5) Tomato Work – &lt;a href=&quot;https://tomato-work.xiejiahe.com/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/xjh22222228/tomato-work&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Tomato-work es un sistema de gestión personal escrito en React con hooks y redux:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/tomato-work-demo.gif&quot; alt=&quot;Live demo, took from README&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Usa &lt;a href=&quot;https://ant.design/&quot;&gt;Antd&lt;/a&gt; para la interfaz! (la librería Antd es genial). La estructura de código es bastante estandard:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/tomato-work-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Encontré interesante que tenga una carpeta llamada services (Angular?) donde cada archivo es una abstracción para hacer llamadas a la API. Así, en vez de llamar a axios directamente en el componente, usa la función del servicio.&lt;/p&gt;

&lt;h2 id=&quot;6-write-with-me--live--repo&quot;&gt;6) Write with me – &lt;a href=&quot;https://www.writewithme.dev&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/dabit3/write-with-me&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Write with me&lt;/em&gt; es un editor de markdown colaborativo construido con React (hooks) y &lt;a href=&quot;https://aws.amazon.com/es/amplify/&quot;&gt;AWS Amplify&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/write-with-me-demo.gif&quot; alt=&quot;Demo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Como podrás ver, es un proyecto pequeño:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/write-with-me-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A diferencia de otros proyectos, el código no está organizado usando distintas carpetas. El manejo del estado de la aplicación está hecho con useReducer.&lt;/p&gt;

&lt;p&gt;La carpeta de graphql es porque Amplify usa graphql.&lt;/p&gt;

&lt;h2 id=&quot;7-jira-clone--live--repo&quot;&gt;7) JIRA Clone – &lt;a href=&quot;https://jira.ivorreic.com/project/board&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/oldboyxx/jira_clone&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Este es un clon simplificado de JIRA. ¡Se ve espectacular!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/jira-clone-demo.png&quot; alt=&quot;Screenshot from the live website&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Usa cypress y jest para testing:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/jira-clone-code-structure.png&quot; alt=&quot;Code structure&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;8-spectrum--live--repo&quot;&gt;8) Spectrum – &lt;a href=&quot;https://spectrum.chat/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/withspectrum/spectrum&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Spectrum es una plataforma de código abierto que ayuda a las personas a construir comunidades online.&lt;/p&gt;

&lt;p&gt;He visto muchas personas recomendando este repositorio. Y hay una buena razón: &lt;strong&gt;el repositorio tiene TODO el código de su producto&lt;/strong&gt;. Esto no siempre pasa.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“That being said, this codebase isn’t your typical open source project because it’s not a library or package with a limited scope—it’s our entire product.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yo creo que este proyecto es uno de los mejores código abierto full-stack en react que hay. Estudiarlo te puede ayudar mucho a como estructurar proyectos con mucho código.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/spectrum-code-structure.png&quot; alt=&quot;Full-Stack JS in action&quot; /&gt;&lt;/p&gt;

&lt;p&gt;El código front que es una SPA (react) vive en la carpeta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/spectrum-code-structure-spa.png&quot; alt=&quot;Spectrum Code Structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can infer from the folder structure, they split the code by “type” of code (components, reducers, hooks, etc).&lt;/p&gt;

&lt;p&gt;Como puedes ver, estructuran el código según “tipo” (components, hooks, etc).&lt;/p&gt;

&lt;h2 id=&quot;9-github-profile-readme-generator--live--repo&quot;&gt;9) GitHub profile README Generator – &lt;a href=&quot;https://rahuldkjain.github.io/gh-profile-readme-generator/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/rahuldkjain/github-profile-readme-generator&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Esta herramienta permite que puedas crear un README para tu perfil de GitHub de manera muy simple y con add-ons como cantidad de visitantes, estrellas en GitHub, etc.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/github-profile-creator-demo.gif&quot; alt=&quot;Project Demo&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Este es un ejemplo de una aplicación construida con &lt;a href=&quot;https://www.gatsbyjs.com/&quot;&gt;Gatsby&lt;/a&gt;. Usa hooks y los archivos están organizados “por tipo” lo cual está bien para el tamaño del proyecto:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/github-profile-generator-code-structure.png&quot; alt=&quot;Code organization&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;10-moose---live--repo&quot;&gt;10) Moose - &lt;a href=&quot;https://getmoose.in/&quot;&gt;Live&lt;/a&gt; | &lt;a href=&quot;https://github.com/ritz078/moose&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Moose es una aplicación para hacer cast, stremear y descargar torrents construida con &lt;a href=&quot;https://github.com/saltyshiomix/nextron&quot;&gt;Nextron&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-demo.png&quot; alt=&quot;Moose Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;¿Qué es Nextron? Bueno es “Next.js” (SSR en aplicaciones React) + “Electron” (para construir aplicaciones de escritorio con herramientas web).&lt;/p&gt;

&lt;p&gt;No tengo experiencia con este tipo de aplicaciones, pero por lo que puedo ver, la organización de código es en base a “tipo de archivos”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-files.png&quot; alt=&quot;Moose file structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Y el proyecto utiliza &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;TypeScript&lt;/a&gt;, y para estilos usa &lt;a href=&quot;https://github.com/css-modules/css-modules&quot;&gt;css-modules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/moose-component-folder.png&quot; alt=&quot;Moose file structure&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;11-courselit---live--repo&quot;&gt;11) Courselit - [Live] | &lt;a href=&quot;https://github.com/codelitdev/courselit&quot;&gt;Repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Courselit es una aplicación web open source para que crees tus propios cursos online. Es una aplicación full-stack, lo que quiere decir que contempla tanto frontend como el backend.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/images/2020-05-10-production-ready-react-apps/courselit-preview.png&quot; alt=&quot;Courselit preview&quot; /&gt;&lt;/p&gt;

&lt;p&gt;El frontend está hecho con &lt;a href=&quot;https://nextjs.org/&quot;&gt;Next&lt;/a&gt; aunque en vez de React utiliza &lt;a href=&quot;https://preactjs.com/&quot;&gt;Preact&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/courselit-files.png&quot; alt=&quot;Courselit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;El backend está con &lt;a href=&quot;https://expressjs.com/&quot;&gt;Express&lt;/a&gt; y &lt;a href=&quot;https://www.mongodb.com/&quot;&gt;MongoDB&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Algo interesante es que el proyecto es un &lt;a href=&quot;https://en.wikipedia.org/wiki/Monorepo&quot;&gt;MonoRepo&lt;/a&gt;, lo que quiere decir que en el mismo repositorio vive el front y el back (ver carpeta &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;packages&lt;/code&gt;) y que para ejecutarlo debes ejecutar los dos proyectos. Para manejar el mono repo, utiliza &lt;a href=&quot;https://github.com/lerna/lerna&quot;&gt;Lerna&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;12-fireact---repo---live&quot;&gt;12) Fireact - &lt;a href=&quot;https://github.com/chaoming/fireact&quot;&gt;Repo&lt;/a&gt; - &lt;a href=&quot;https://fireact-e1bdc.firebaseapp.com/&quot;&gt;Live&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Fireact es una aplicación para construir tu propio &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_as_a_service&quot;&gt;SaaS&lt;/a&gt; y que ya viene lista para recibir pagos mediante &lt;a href=&quot;https://stripe.com/es-us&quot;&gt;Stripe&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;En términos técnicos, es una app full-stack construida con &lt;a href=&quot;https://firebase.google.com/&quot;&gt;Firebase&lt;/a&gt; y React (&lt;a href=&quot;https://github.com/facebook/create-react-app&quot;&gt;CRA&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;La estructura de carpetas es una mezcla entre “tipo de archivos”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/fireact-files.png&quot; alt=&quot;Fireact code structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Y por páginas, las cuales están divididas en “páginas con autenticación” y “páginas sin autenticación”:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-05-10-production-ready-react-apps/fireact-pages.png&quot; alt=&quot;Fireact page separation&quot; /&gt;&lt;/p&gt;

&lt;p&gt;En cuanto a Firebase, utiliza &lt;a href=&quot;https://firebase.google.com/docs/firestore&quot;&gt;Firestore&lt;/a&gt;, como pueden ver en el archivo &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserList&lt;/code&gt; (&lt;a href=&quot;https://github.com/chaoming/fireact/blob/master/src/pages/auth/accounts/UserList/index.js#L39&quot;&gt;aquí&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Y finalmente, la interfaz utilizó &lt;a href=&quot;https://coreui.io&quot;&gt;coreui&lt;/a&gt; que es un template de administración para Bootstrap:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;../assets/images/2020-05-10-production-ready-react-apps/fireact-screenshot.png&quot; alt=&quot;Fireact Screenshot&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;olvidé-algún-proyecto&quot;&gt;¿Olvidé algún proyecto?&lt;/h2&gt;

&lt;p&gt;Fue un poco complicado encontrar proyectos open source que utilizaran hooks y últimas cosas de React 😓.&lt;/p&gt;

&lt;p&gt;Si tienes alguno, puedes escribirme por Twitter a &lt;a href=&quot;https://twitter.com/codewithnico&quot;&gt;@codewithnico&lt;/a&gt;&lt;/p&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="es" /><category term="react" /><summary type="html">Aquí un listado de aplicaciones React modernas open source para estudiar</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-05-10-production-ready-react-apps/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-05-10-production-ready-react-apps/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry xml:lang="en"><title type="html">How to wait for Axios data before you render it?</title><link href="https://codewithnico.com/react-wait-axios-to-render/" rel="alternate" type="text/html" title="How to wait for Axios data before you render it?" /><published>2020-04-29T00:00:00+00:00</published><updated>2020-04-29T00:00:00+00:00</updated><id>https://codewithnico.com/react-wait-axios-to-render</id><content type="html" xml:base="https://codewithnico.com/react-wait-axios-to-render/">&lt;blockquote&gt;
  &lt;p&gt;Getting data with Axios is easy, but rendering it ~OUCH~&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may know how to fetch data with Axios, but how do you render it in React?&lt;/p&gt;

&lt;p&gt;Maybe you tried something like using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;await&lt;/code&gt; in render function to make React wait for your request before rendering. But when you do something like:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;await&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;axios&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(..)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;await in render()&lt;/figcaption&gt;

&lt;p&gt;You’ll get an error like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/error.png&quot; alt=&quot;Error when using await in render()&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Error when using await in render()&lt;/figcaption&gt;

&lt;p&gt;Why? Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;async&lt;/code&gt; operator makes the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;render&lt;/code&gt; function to return a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Promise&lt;/code&gt;, which is an object and React doesn’t know to render objects.&lt;/p&gt;

&lt;p&gt;So, how do you make React wait for your function before render?&lt;/p&gt;

&lt;p&gt;Well, the answer is: faking it 😏&lt;/p&gt;

&lt;h2 id=&quot;waiting-for-axios-before-rendering&quot;&gt;Waiting for Axios before rendering&lt;/h2&gt;

&lt;p&gt;There is a &lt;em&gt;fetching recipe&lt;/em&gt; for doing this which is always the same:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;Start your component in “loading mode”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;*When your component “mounts” *do the request.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;When the request is done&lt;/em&gt;, save your data and turn off “loading mode”.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Make sure that while your component is in “loading mode”&lt;/em&gt;, display a spinner or loading text in your render function. And when not, display your data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;How do you do this in code?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Look at this simple React app that fetches data about a single Pokémon and displays its name and picture:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/demo.gif&quot; alt=&quot;Pokémon Demo&quot; /&gt;&lt;/p&gt;

&lt;figcaption&gt;Example code can be found &lt;a href=&quot;https://codesandbox.io/s/wait-for-axios-before-render-in-react-h1js8?file=/src/App.js&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/figcaption&gt;

&lt;p&gt;Here is the implementation:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;setPokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;useState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;useEffect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;axios&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;https://pokeapi.co/api/v2/pokemon/4&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;setPokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;setLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sprites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;front_default&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Load, fetch and display!&lt;/p&gt;

&lt;p&gt;How is this translated to our recipe?&lt;/p&gt;

&lt;h3 id=&quot;1-start-your-component-in-loading-mode&quot;&gt;1) Start your component in “loading mode”&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/step1.png&quot; alt=&quot;How to start your component in loading mode&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;This way you start your component in &quot;loading mode&quot;&lt;/figcaption&gt;

&lt;p&gt;With the conditional rendering of &lt;em&gt;“Loading…”&lt;/em&gt; text and by using useState we accomplish the “loading mode” state of the component.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;**Why are we using useState instead of a simple const? **Because we want to make App component remember this value between renders!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first render displays &lt;em&gt;“Loading…”&lt;/em&gt; in the browser.&lt;/p&gt;

&lt;h3 id=&quot;2-when-your-component-mounts-do-the-request&quot;&gt;2) When your component “mounts” do the request&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/step2.png&quot; alt=&quot;How to make a request after your component mounts&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Mounts → &quot;after render&quot; → accomplished with useEffect&lt;/figcaption&gt;

&lt;p&gt;&lt;em&gt;useEffect&lt;/em&gt; receives a function that will be called after the DOM (what the browser shows) is updated. Here we tell axios to fetch Charmander data from PokeApi.&lt;/p&gt;

&lt;p&gt;axios.get is executed asynchronous, so nothing is changed.&lt;/p&gt;

&lt;h3 id=&quot;3-when-the-request-is-done-save-your-data-and-turn-off-loading-mode&quot;&gt;3) When the request is done, save your data and turn off “loading mode”&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/step3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;figcaption&gt;When request is done, we update the state of our component by using &lt;code&gt;setLoading&lt;/code&gt; and &lt;code&gt;setPokemon&lt;/code&gt;.&lt;/figcaption&gt;

&lt;p&gt;Both calls update the state:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setPokemon&lt;/code&gt; updates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pokemon&lt;/code&gt;  from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;response.data&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setLoading&lt;/code&gt; updates &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isLoading&lt;/code&gt; from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So… there are 2 renders! 🤯&lt;/p&gt;

&lt;p&gt;The second render (triggered by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setPokemon&lt;/code&gt;) will do nothing in the browser because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isLoading&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; → component returns &lt;em&gt;“Loading…”&lt;/em&gt; again.&lt;/p&gt;

&lt;p&gt;The third render (triggered by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setLoading&lt;/code&gt;) will show Charmander in the browser! Because this time &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isLoading&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, so we reach the end of the function were the pokemon is &lt;em&gt;finally&lt;/em&gt; rendered.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summing-up&lt;/strong&gt; this confusing render stuff:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/2020-04-29-react-wait-axios-to-render/summary.png&quot; alt=&quot;Summary&quot; /&gt;&lt;/p&gt;
&lt;figcaption&gt;Adding comments in key parts of the code can teach us few things :)&lt;/figcaption&gt;

&lt;h2 id=&quot;not-familiar-with-hooks&quot;&gt;Not familiar with hooks?&lt;/h2&gt;

&lt;p&gt;Hey! Is 2020, you should! 😂&lt;/p&gt;

&lt;p&gt;No problem. Here is the equivalent code with class component:&lt;/p&gt;

&lt;div class=&quot;language-jsx highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Component&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;componentDidMount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;After mount! Let's load data from API...&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;axios&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;https://pokeapi.co/api/v2/pokemon/4&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;setState&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nx&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;isLoading&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;className&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;App&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;h1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;alt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;src&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;pokemon&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;sprites&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;front_default&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;figcaption&gt;Code using class components in &lt;a href=&quot;https://codesandbox.io/s/wait-for-axios-before-render-in-react-ulz39?file=/src/App.js&quot; target=&quot;_blank&quot;&gt;CodeSandbox&lt;/a&gt;&lt;/figcaption&gt;</content><author><name>Nico</name><email>ngomez@hey.com</email></author><category term="React" /><summary type="html">You may know how to fetch data with Axios, but how do you render it to React?</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://codewithnico.com/assets/images/2020-04-29-react-wait-axios-to-render/cover.png" /><media:content medium="image" url="https://codewithnico.com/assets/images/2020-04-29-react-wait-axios-to-render/cover.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>