{"id":1710,"date":"2024-08-02T11:16:37","date_gmt":"2024-08-02T09:16:37","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1710"},"modified":"2024-08-02T11:16:38","modified_gmt":"2024-08-02T09:16:38","slug":"003-interfaz-de-usuario-ui-basada-en-el-estado","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/19-reactividad\/003-interfaz-de-usuario-ui-basada-en-el-estado\/","title":{"rendered":"003. Interfaz de Usuario (UI) basada en el Estado"},"content":{"rendered":"\n<p>En el cap\u00edtulo anterior hicimos una <strong>manipulaci\u00f3n no reactiva<\/strong>, es decir, com\u00fan y corriente de <strong>elementos del DOM<\/strong>, en este cap\u00edtulo vemos como ser\u00e1 hacer la transici\u00f3n de una manipulaci\u00f3n com\u00fan a una manipulaci\u00f3n de la <strong>interfaz basada en un estado<\/strong> (datos de nuestra aplicaci\u00f3n en cierto momento).<\/p>\n\n\n\n<p>Vamos a crear un archivo denominado <em>01_ui-basada-estado.html,<\/em> y dentro de este archivo vamos a crear una variable basada en el estado, a la que los desarrolladores denominan <strong>state.<\/strong> Tendremos que tener un mecanismo que genere esa interfaz basada en el estado, es decir, un <strong>template<\/strong>&nbsp;<strong>UI,<\/strong> por lo que crearemos una variable que permita trabajar un <strong>template UI<\/strong>, se tratar\u00e1 de una funci\u00f3n flecha que va a interactuar con las listas que tiene el <strong>estado,<\/strong> es decir, con la propiedad <em>todoList,<\/em> y por cada elemento que tenga, va a generar una <strong><em>&lt;li&gt;.<\/em><\/strong> Tambi\u00e9n necesitamos un proceso que va a hacer el <strong>renderizado UI.<\/strong> Y lo \u00faltimo es actualizar el estado <strong>(state)<\/strong> y la <strong>UI.<\/strong><\/p>\n\n\n\n<p>El siguiente paso que queda es hacer que nuestro estado sea realmente reactivo.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Vamos un crear un nuevo archivo <em>01_ui-basada-estado.html,<\/em> que va a tener la siguiente sintaxis.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"es\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;Interfaz de usuario basada en el estado&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Interfaz de usuario basada en el estado&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;form id=\"todo-form\"&gt;\n&nbsp; &nbsp; &nbsp; &lt;input type=\"text\" id=\"todo-item\" placeholder=\"Tarea por hacer\" \/&gt;\n&nbsp; &nbsp; &nbsp; &lt;input type=\"submit\" value=\"Agregar\" \/&gt;\n&nbsp; &nbsp; &lt;\/form&gt;\n\n&nbsp; &nbsp; &lt;h2&gt;Lista de tareas&lt;\/h2&gt;\n\n&nbsp; &nbsp; &lt;ul id=\"todo-list\"&gt;&lt;\/ul&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; const d = document;\n\n&nbsp; &nbsp; &nbsp; \/\/ El State\n&nbsp; &nbsp; &nbsp; const state = {\n&nbsp; &nbsp; &nbsp; &nbsp; todoList: [],\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; \/\/ Template\n&nbsp; &nbsp; &nbsp; const template = () =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (state.todoList.length &lt; 1) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return `&lt;p&gt;&lt;em&gt;Lista sin tareas por hacer&lt;\/em&gt;&lt;\/p&gt;`;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n\n&nbsp; &nbsp; &nbsp; &nbsp; let todos = state.todoList.map((item) =&gt; `&lt;li&gt;${item}&lt;\/li&gt;`).join(\"\");\n&nbsp; &nbsp; &nbsp; &nbsp; return todos;\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; \/\/ Render UI\n&nbsp; &nbsp; &nbsp; const render = () =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(state);\n&nbsp; &nbsp; &nbsp; &nbsp; const $list = d.getElementById(\"todo-list\");\n&nbsp; &nbsp; &nbsp; &nbsp; if (!$list) return;\n&nbsp; &nbsp; &nbsp; &nbsp; $list.innerHTML = template();\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; d.addEventListener(\"DOMContentLoaded\", render);\n\n&nbsp; &nbsp; &nbsp; d.addEventListener(\"submit\", (e) =&gt; {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!e.target.matches(\"#todo-form\")) return false;\n\n&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();\n\n&nbsp; &nbsp; &nbsp; &nbsp; const $item = d.getElementById(\"todo-item\");\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (!$item) return;\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Actualizar el State y la UI\n&nbsp; &nbsp; &nbsp; &nbsp; state.todoList.push($item.value);\n&nbsp; &nbsp; &nbsp; &nbsp; render();\n\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ Limpiar el input\n&nbsp; &nbsp; &nbsp; &nbsp; $item.value = \"\";\n&nbsp; &nbsp; &nbsp; &nbsp; $item.focus();\n&nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En el cap\u00edtulo anterior hicimos una manipulaci\u00f3n no reactiva, es decir, com\u00fan y corriente de elementos del DOM, en este [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1703,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1710","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En el cap\u00edtulo anterior hicimos una manipulaci\u00f3n no reactiva, es decir, com\u00fan y corriente de elementos del DOM, en este [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1710"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1710\/revisions"}],"predecessor-version":[{"id":1712,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1710\/revisions\/1712"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1703"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}