8bitzz / blogs

0 stars 0 forks source link

A study on client-side frameworks (as a beginner) Part II #31

Open 8bitzz opened 2 years ago

8bitzz commented 2 years ago

2. Framework main features

Reference: Framework main features

2.1 Domain-specific languages

All frameworks allow developers to use domain-specific languages (DSLs) in order to build web applications. DSLs can't be read by the browser directly; they must be transformed into JavaScript or HTML first using Transformation tools. However, framework tooling generally includes the required tools to handle this step, or can be adjusted to include this step. When embracing DSLs, it would be easier to find help from the communities around those frameworks

Example of JSX syntax as the DSL of ReactJS framework

  1. The curly braces around subject on line 4 tell the application to read the value of the subject constant and insert it into our h1
    const subject = "World";
    const header = (
    <header>
    <h1>Hello, {subject}!</h1>
    </header>
    );
  2. When used with React, the JSX from the previous snippet would be compiled into this:
    var subject = "World";
    var header = React.createElement("header", null,
    React.createElement("h1", null, "Hello, ", subject, "!")
    );
  3. When ultimately rendered by the browser, the above snippet will produce HTML that looks like this
    <header>
    <h1>Hello, World!</h1>
    </header>

    2.2 Writing components

    Most frameworks have some kind of component model. Each framework's components offer a way to describe

    • the external properties they may need
    • the internal state that the component should manage
    • and the events a user can trigger on the component's markup

Example of writing a Component with props, states and events using ReactJS framework

Properties

Properties, or props, are external data that a component needs in order to render.

  1. Define a representation of the Component with props, given where our props will be inserted into the component
    function AuthorCredit(props) {
    return (
    <figure>
      <img src={props.src} alt={props.alt} />
      <figcaption>{props.byline}</figcaption>
    </figure>
    );
    }
  2. Render a specific component (which will probably be inside another component) by giving it their props
    <AuthorCredit
    src="./assets/zelda.png"
    alt="Portrait of Zelda Schiff"
    byline="Zelda Schiff is editor-in-chief of the Library Times."
    />
  3. When ultimately rendered by the browser, the above snippet will produce HTML that looks like this
    <figure>
    <img
    src="assets/zelda.png"
    alt="Portrait of Zelda Schiff"
    >
    <figcaption>
    Zelda Schiff is editor-in-chief of the Library Times.
    </figcaption>
    </figure>

State

  1. Use useState Hook to give an initial data value, will keep track of that value as it is updated
    function CounterButton() {
    const [count] = useState(0);
    return (
    <button>Clicked {count} times</button>
    );
    }
  2. When ultimately rendered by the browser, the above snippet will produce HTML that looks like this
    <button>Clicked 0 times</button>

Event

  1. React listen to the onClick event, then use the useState hook to create the setCount function to update the state count
    function CounterButton() {
    const [count, setCount] = useState(0);
    return (
    <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
    );
    }
  2. More info React useState Hook: How does setState know what to do

2.3 Styling components

  1. Use an entirely different language and have it transformed into a web-compatible language

    • Sass/SCSS: This CSS extension allows developers to use variables, nested rules, mixins, functions, and many other features, some of which are available in native CSS (such as variables), and some of which aren't.
  2. Write code using the latest language features and have that transformed into code that works on everyday devices

    • PostCSS: Transpile the CSS stylesheets. If there isn’t an equivalent way to do something using older CSS features, PostCSS will install a JavaScript polyfill to emulate the CSS effect.
  3. Use UI-libraries for frameworks

    • TailwindCSS, Material UI, Chakra UI, etc: Provide ready-to-use components for frameworks

2.4 Handling dependencies

Dependency injection

import AuthorCredit from "./components/AuthorCredit";

<App>
  <Home>
    <Article>
      <AuthorCredit {/* props */} />
    </Article>
  </Home>
</App>

LifeCycle

2.5 Rendering Elements

Document Object Model (DOM)

Virtual DOM