State derived frontends
Course: DOM Part 2 - chapter 5
To make frontends easier to code, the concept of state came into the picture. You will see this more when we reach react.
There are three jargon we need to understand
- State - The
variableparts of an app. - Components - How to
renderstateon screen. - Rendering - Taking the
stateand rendering it on theDOMbased on thecomponents
TODO App
Section titled “TODO App”const todos = [{ id: 1, description: "Go to gym"}, { id: 2, description: "Eat food"}];Component
Section titled “Component”function todoComponent(todo) { const div = document.createElement("div"); const h1 = document.createElement("h1"); const button = document.createElement("button"); button.innerHTML = "Delete"; h1.innerHTML = todo.title; div.appendChild(h1); div.appendChild(button);}
Linkedin Topbar
Section titled “Linkedin Topbar”
const state = { notifications: { home: 0, myNetwork: "99+", jobs: 0, messaging: 0, notifications: 25 }, profilePicture: "https://media.licdn.com/dms/image/v2/C5603AQFbOqG9og1S5g/profile-displayphoto-shrink_100_100/profile-displayphoto-shrink_100_100/0/1517251238138?e=1729728000&v=beta&t=xHUuE_3gkUPXYajsv8fk_kv37oB49Mqbi20IVAjn_rw"}Components
Section titled “Components”
Started code
Section titled “Started code”<body> <input type="text"></input> <button onclick="addTodo()">Add todo!</button> <script> let todos = []; function addTodo() { todos.push({ title: document.querySelector("input").value }) render(); }
function deleteTodo() {
render(); }
function render() {
} </script></body>