4.5.3 • Published 3 months ago

untrue v4.5.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Untrue

JavaScript library for rendering user interfaces.

Installation

The easiest way to get started with Untrue is through a web app.

npm i untrue @untrue/web

Compatible with any build tool: Parcel, Vite, Webpack, etc.

Native app development coming soon.

Get started

You can add Untrue to any part of your page.

import $ from "untrue";

import { Tree } from "@untrue/web";

import App from "./App";

const root = document.getElementById("root");

const tree = new Tree(root);

// $ is a shorthand to represent nodes

tree.mount($(App));

In this case, we're adding Untrue to #root.

More on App in the next section.

Basic features

Interactivity

A component state can change at any time and Untrue knows which nodes should be updated in the DOM.

import $, { Component } from "untrue";

class App extends Component {
  constructor(props) {
    super(props);

    this.state = { counter: 0 };
  }

  onIncrement = () => {
    const { counter } = this.state;

    this.updateState({ counter: counter + 1 });
  };

  render() {
    // after the first click, counter is no longer 0 but 1

    const { counter } = this.state;

    // regular arrays are used to return multiple nodes

    return [
      $("span", counter),
      $("button", { onclick: this.onIncrement }, "increment"),
    ];
  }
}

export default App;

The output HTML will be:

<span>0</span> <button>increment</button>

button will have an onclick listener attached to it.

span will be updated with the new counter every time button is clicked.

Modularity

Components can be classes or functions and are used to group multiple nodes.

import $, { Component } from "untrue";

function App() {
  return [
    $(Header, { title: "Untrue" }), // pass title as prop (external data)
    $(Footer, { year: 2049 }), // pass year as prop (external data)
  ];
}

class Header extends Component {
  constructor(props) {
    super(props);

    this.state = { counter: 0 };
  }

  onIncrement = () => {
    const { counter } = this.state;

    this.updateState({ counter: counter + 1 });
  };

  render() {
    const { title } = this.props; // external data

    const { counter } = this.state; // internal data

    return $("header", [
      $("h1", title),
      $("span", counter),
      $("button", { onclick: this.onIncrement }, "increment"),
    ]);
  }
}

function Footer({ year }) {
  return $("footer", [
    $("span", `copyright, ${year}`),
    $("a", { href: "https://example.com" }, "follow me"),
  ]);
}

export default App;

The output HTML will be:

<header>
  <h1>Untrue</h1>
  <span>0</span>
  <button>increment</button>
</header>
<footer>
  <span>copyright, 2049</span>
  <a href="https://example.com">follow me</a>
</footer>

Header will be stateful while Footer will be stateless. Both components receive props.

Lifecycle events

  • render: Every render, whether it's a mount or an update.
  • mount: The first render.
  • update: Every rerender, whether it's caused by the component itself or a parent component.
  • unmount: Component is no longer part of the Tree.

Multiple event listeners can be attached to a single event. Specially useful to have more organized code.

import $, { Component } from "untrue";

class App extends Component {
  constructor(props) {
    super(props);

    this.state = { counter: 0 };

    this.interval = null;

    // start interval on mount

    this.on("mount", () => {
      this.interval = setInterval(() => {
        const { counter } = this.state;

        this.updateState({ counter: counter + 1 });
      }, 5000);
    });

    // clear interval on unmount

    this.on("unmount", () => {
      clearInterval(this.interval);
    });

    // check "counter" change on update

    this.on("update", () => {
      // this.props and this.prevProps are also available

      const { counter } = this.state;
      const { counter: prevCounter } = this.prevState;

      if (counter !== prevCounter) {
        alert("Counter has been updated.");
      }
    });
  }

  render() {
    const { counter } = this.state;

    return $("span", counter);
  }
}

export default App;

The output HTML will be:

<span>0</span>

An alert will happen every 5 seconds because interval updates counter on every call.

4.5.3

3 months ago

4.5.2

4 months ago

4.5.0

4 months ago

4.5.1

4 months ago

4.4.5

4 months ago

4.4.1

9 months ago

4.0.5

10 months ago

4.4.0

9 months ago

4.0.4

10 months ago

4.4.3

7 months ago

4.4.2

9 months ago

4.0.6

10 months ago

4.0.1

10 months ago

4.0.0

10 months ago

4.0.3

10 months ago

4.0.2

10 months ago

4.4.4

7 months ago

4.3.2

9 months ago

4.3.1

9 months ago

4.3.0

9 months ago

3.11.4

11 months ago

3.11.3

11 months ago

3.11.6

11 months ago

3.9.0

11 months ago

3.11.8

10 months ago

3.11.7

10 months ago

3.11.9

10 months ago

3.10.1

11 months ago

3.8.0

11 months ago

3.10.0

11 months ago

3.10.3

11 months ago

3.10.2

11 months ago

4.2.1

9 months ago

4.2.0

9 months ago

3.11.0

11 months ago

3.11.1

11 months ago

4.1.0

9 months ago

4.1.1

9 months ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.4

1 year ago

3.7.4

12 months ago

3.7.3

12 months ago

3.7.2

12 months ago

2.0.1

1 year ago

2.0.0

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.6.1

1 year ago

3.6.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

1.1.19

1 year ago

3.1.1

1 year ago

1.1.18

1 year ago

3.1.0

1 year ago

1.1.17

1 year ago

3.1.7

1 year ago

3.1.6

1 year ago

3.1.5

1 year ago

3.5.0

1 year ago

3.1.4

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

3.1.8

1 year ago

3.4.0

1 year ago

3.4.1

1 year ago

3.0.0

1 year ago

1.0.33

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

3.3.0

1 year ago

3.7.1

1 year ago

3.7.0

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.0.0

1 year ago