We are going to learn how to use a framework (like React/Vue) by building one ourselves. This approach gives you two benefits:
- You get to learn how to use a framework since you understand the principles behind it.
- You get to see how to use Vanilla JavaScript to build something complex, which will then bolster your confidence in JavaScript going forward
Weโre going to build two things together at the same time:
- The Framework
- A Counter App
The Counter App
The Counter app weโre building contains three parts:
- The Parent Counter
- The Child Counter
- The Total Count
It looks like this:
This app doesnโt make sense for a normal user, but itโs perfect for illustrating what goes behind a framework.
You can find the HTML and CSS for the Counter App in the Starter files.
The Setup
Weโre going to call the framework โTinyโ since itโs a small framework thatโs not recommended for real-world usage. (Youโll understand why itโs not recommended for real-world usage as you go through the lessons).
Weโll begin by creating a tiny.js file. I like to put this inside a folder called Tiny so itโs separate from the Counter app weโre building.
We can then include Tiny into main.js like this:
import Tiny from './Tiny/tiny.js'Then we need to export Tiny from tiny.js so main.js can use it.
export default function Tiny() {}The Initial Render
The first thing a framework needs to do is to allow users to render their components onto the DOM. This means users give us the HTML and we render it into the DOM.
We need two things to do this:
- The element to render into
- The HTML to render
For the element, we make it easier for users by asking them to pass in a selector.
Tiny({ selector: 'main',})We can then find the selector in Tiny with document.querySelector.
export default function Tiny(options) { const element = document.querySelector(options.selector)}We also want to give users the option to pass in the actual element if the element is hard to find via a simple querySelector.
We can use the same selector option by checking if the selector is a string. If selector is a string, we know we need to run document.querySelector. Otherwise, we know weโve been given an element.
export default function Tiny(options) { const element = typeof options.selector === 'string' ? document.querySelector(options.selector) : options.selector}Next we need the HTML to render.
We can pass a template property into the Tiny for the HTML.
Tiny({ selector: 'main', template: 'Hello World',})We can then render the template by setting the elementโs innerHTML property.
export default function Tiny(options) { // ... function _render() { element.innerHTML = options.template }
_render()}
Although itโs possible to pass a string as the template, we usually donโt do this. Itโs better to use a method instead because a method allows users to use other properties that are declared within the component. This feature will become very useful soon.
Tiny({ selector: 'main', template() { return `I'm being rendered onto ${this.selector}` },})To support this feature, we need to run the template method in Tiny.js.
export default function Tiny(options) { // ... function _render() { element.innerHTML = options.template() }
_render()}
Note: Did you notice this is a callback pattern? ๐. template is a function thatโs passed into another function (Tiny) to be executed later.
Rendering the Counter App
You can find the HTML in the Starter files. What we need to do now is to render the entire HTML into the DOM. Hereโs the necessary code.
Tiny({ // ... template() { return ` <div class="component parent-component flow"> <h1>Parent Counter</h1> <p>Count: 0</p> <button>Increase count by 1</button>
<div class="half"> <div class="component child-component flow"> <h2>Child Counter</h2> <p>Count: 0</p> <button>Increase count by 1</button> <button>Increase parent count by 1</button> </div> <div class="component total-component flow text"> <h2>Total Count</h2> <ul> <li>Parent Count: 0</li> <li>Child Count: 0</li> <li>Total Count: 0</li> </ul> </div> </div> </div>` },})Thatโs it! You should now see the counter app in the DOM. But this time, itโs rendered entirely with JavaScript.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson