Logo

dev-resources.site

for different kinds of informations.

MithrilJS component with state management

Published at
5/29/2024
Categories
mithriljs
component
state
Author
pablo_74
Categories
3 categories in total
mithriljs
open
component
open
state
open
Author
8 person written this
pablo_74
open
MithrilJS component with state management

This is my 11th write up on dev.to site. MithrilJS component and state management. A substantial part of the code was written by AI. Source code included.

MithrilJS

Mithril is an excellent JS framework, tiny and powerful. Like with another JS frameworks you can write components - a small piece of code solving a dedicated subtask.

State management

Most of applications and most of JS frameworks implement state management - the way state is realized and maintained.

There are many ways to implement state management in Mithril. Excellent source of information is a blog post of Kevin Fiol: Simple State Management in Mithril.js

Component

In this example I have SVG component, logical OR gate. It has two inputs (A, B) and one output (Q).
You can intend either internal component state (so component is independent) or global app state. And this is what I show you in this example.

State

const NodeState = (size) => Array(size).fill(false);

const NodeActions = (state) => ({
  setNode: (index, value) => {
    if (index >= 0 && index < state.length) {
      state[index] = value;
      m.redraw();  // Ensure Mithril redraws the view to reflect state changes
    }
  }
});
Enter fullscreen mode Exit fullscreen mode

OR gate

const ORGate = {
  view: ({ attrs: { state, actions, idxA, idxB, idxQ } }) => {
    const inputA = state[idxA];
    const inputB = state[idxB];
    const output = inputA || inputB;

    // Update the output node state
    actions.setNode(idxQ, output);

    return m('div',
      m('svg', {
          viewBox: '0 0 100 100',
          width: '100',
          height: '100',
          xmlns: 'http://www.w3.org/2000/svg'
        },
        [
          // Input lines
          m('line', { onclick: () => actions.setNode(idxA, !inputA), x1: '0', y1: '30', x2: '30', y2: '30', stroke: inputA ? 'red' : 'blue', 'stroke-width': '2' }),
          m('line', { onclick: () => actions.setNode(idxB, !inputB), x1: '0', y1: '70', x2: '30', y2: '70', stroke: inputB ? 'red' : 'blue', 'stroke-width': '2' }),

          // OR gate rectangle
          m('rect', { x: '30', y: '20', width: '40', height: '60', fill: 'none', stroke: 'black', 'stroke-width': '2' }),

          // Output line
          m('line', { x1: '70', y1: '50', x2: '100', y2: '50', stroke: output ? 'red' : 'blue', 'stroke-width': '2' }),

          // Labels for inputs and output
          m('text', { onclick: () => actions.setNode(idxA, !inputA), x: '13', y: '25', 'text-anchor': 'middle', 'font-size': '12' }, 'A = ', inputA ? '1' : '0'),
          m('text', { onclick: () => actions.setNode(idxB, !inputB), x: '13', y: '65', 'text-anchor': 'middle', 'font-size': '12' }, 'B = ', inputB ? '1' : '0'),
          m('text', { x: '86', y: '40', 'text-anchor': 'middle', 'font-size': '12' }, 'Q = ', output ? '1' : '0'),
          // sign in gate
          m('text', {x: '45', y: '40', 'text-anchor': 'right', 'font-size': '18'}, "≥1")
        ]
      ),
    );
  }
};
Enter fullscreen mode Exit fullscreen mode

Short explanation

There is an array of values (initially: false values) called nodes. Nodes are used as fictive nodes in real logical circuit. Every gate has two inputs ("connected" to nodes) and one output ("connected" to node). All nodes are in one array; index is used to work with specific node (item of array). Indexes of nodes for inputs and output are put to the component via attributes.

m(ORGate, { state: nodeState, actions: nodeActions, idxA: idxA, idxB: idxB, idxQ: 2 }),
Enter fullscreen mode Exit fullscreen mode

Source code

Complete source code with visualization is on flems playground.
Hope this is useful for you, developers. A little note: chatGPT AI ​​did most of the work.

component Article's
30 articles in total
Favicon
Build a note app with JavaScript component.
Favicon
Key characteristic of Component-Based Architecture
Favicon
React Component Libraries: Overview of 19 Top Libs
Favicon
Styling Components in React 🧢
Favicon
Building a Seamless OTP Input Field in React: A Step-by-Step Guide
Favicon
MithrilJS component with state management
Favicon
[Off Topic] Nano introdução do framework Angular para Devs do back
Favicon
Comparing Vue Component Documentation tools
Favicon
Laravel 11 Custom Component File Structure
Favicon
Building Message Component in Vue3
Favicon
Aplicando paginação em um componente Select
Favicon
How much does it cost to repair an outdoor LED display?
Favicon
Global toast in Vue3
Favicon
Unveiling the Hidden Gem of React: The Power of Compound Components
Favicon
Controlled vs Uncontrolled Components in React
Favicon
React components -(Class component v/s function component)
Favicon
3 Ways to Create React Components with Bit
Favicon
Client or Server component ?
Favicon
Desenvolvimento de Componentes Assíncronos com Vue.js
Favicon
NAND Flash vs NOR Flash: Differences between them
Favicon
Link Component in React Router
Favicon
Guia de Components - para quem tem pressa!
Favicon
How to exchange data between Angular components
Favicon
Component Testing with Cypress and Reactjs
Favicon
React - Higher Order Components (HOC)
Favicon
How to Create Component Library Like Material UI or Mantine UI?
Favicon
Looking forward to adding Algolia's DOCSEARCH to Mantine DataTable
Favicon
Cypress Component Testing vs React Testing Library - the complete comparison
Favicon
Creating Custom Component for NPS Feedback
Favicon
Maximize your Angular code reusability using <NgTemplateOutlet>

Featured ones: