Skip to content

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

  1. State - The variable parts of an app.
  2. Components - How to render state on screen.
  3. Rendering - Taking the state and rendering it on the DOM based on the components
const todos = [{
id: 1,
description: "Go to gym"
}, {
id: 2,
description: "Eat food"
}];
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);
}

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"
}

<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>