JSX is not your Javascript friend, and do you really need it ?
Published: 2022-09-27 13:44:01

JSX is certainly and awesome tool, but this as a cost.

1. It does not allow comments
2. It does not allow intermix Javascript code.
3. It must be transpiled with babel
4. ...

Of course there are alernatives, like 'html' tag templates. But they also suffer some of the JSX problems.

Suppose we have to create a "grid of numbers" component and have to display two components of this type in a legacy app.

Would you use React, Angular, Vue, Solid...?

Here is a solution in plain Javascript. 

```html
<script src="https://cdn.jsdelivr.net/gh/artydev/mvu@2.0/dist/mvu.umd.js"></script>

<h2>
  A simple grid with MVU
</h2>

<div id='app'>
</div>

<script>
  const {dom, udom, m, render } = MVU;

  const cellStyle = `
    text-align:center;
    padding-top:10px;
    display:inline-block;
    width:50px;
    height:50px;
    color:white;
    font-weight:bold;
    cursor:pointer`;

  let div = m("div"); // create a div factory
  let br = m("br");   // create  a br factory
  let hr = m("hr");   // create  a hr factory
  let h2 =  m("h2");  // create  a h2 factory
  
  let selectedCell = div();
  
  let cell = (i) => div(i, {style: cellStyle});

    function App (numgrid, bgcolor="deeppink") {
    let app = dom();   // here we wrap our grid component between (dom(), udom() <=> <div></div>)    
      for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
          let content = String(j + 10*i)
          let c = cell(content); 
          c.onclick = () => 
            selectedCell.innerHTML = `<h2>Selected cell : ${content} in grid n° ${numgrid}</h2>`
        }
        br()
      }
    udom();
    app.style= `width:500px;background:${bgcolor};`
    return app
  }


  function main () {
    render(
      document.getElementById("app"), 
      selectedCell,
      App(1,"green"),
      br(),
      App(2, "orange"),
    )
  }

  main()
</script>
```
You can test it here : [Grids](https://flems.io/#0=N4IgtglgJlA2CmIBcA2ALAOgKwGYA0IAzvAgMYAu8UyIGAFuWLCAYaQE4D2szSA2gAY8AgLoEAZhASFkfUADsAhmERJaDJixClO8ynpoAeNuwgAHcgAJC7UgF4AOiAbkzhJAHoPpKPIwArQigSCAA3dgx5eHIPAHM6D0V2cgBPYNCPMFCAVwABACYMAQ8oCEIYrOyMbLAo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HlaJ4LEaLReZwtEaCwABOJA7YpAQkAEEAoTwBRlFUWhAi0ZbyBod3PZAJQVBoDBSEIGQCADoOoTEUPvZoDZNhfQHYxkKEgA)

And here is another demo :

```js
const {dom, udom, m, render } = MVU;

let h1 = m("h1")
let button = m("button")

const numCounters = 5

const state = {
  counters : [...Array(numCounters)].map(() => 0)  
}

const Counter = (state, num) => {
  h1(`Counter : ${state.counters[num]}`)
  let incButton = button("inc");
  incButton.onclick = () =>  { state.counters[num]++;  updateApp(state) }
} 

function App (state) {
  let view = dom();
    h1("This is a basic demo");
    [...Array(numCounters)].map((_,index) => Counter(state, index));
  udom();
  return view
}

function updateApp (state) {
    render(app, App(state))
}


updateApp(state)

```
You can test it here : [Counters](https://flems.io/#0=N4IgzgxgTg9gNnEAuA2gBgDRoLoZAMwEs4BTMZFUAOwEMBbE5EAOgAsAXOxPCGK9kvyYAeSFEIAHdgAIwUCAF4AOiA7sJYJAHotEACZVmAKzB6ScQgDcozKiXZaA5qy00o7AJ5nLWupYCuAAIATMxoWnqEYA5+-sz+dHrGYCoAfMJaYpLsqUpUecKRltKEegoA5DQSEuW5VNLSBRFWqSB4YOYkEOyEfORIIADMSMEgAL4Y1PSMA8ltILz8guxMi9HSwHowdBjS-ls70odQgmZQ0mPSCtIAsgBqAKoA3Hl5pDKsAIxXRwAUKl8VABKN72aQAI387HYfB+dH+IEh0L4wNeVDWMioCQAwjB-EsoGAfgBWNEY2TsGgCH7APINXj4gSE6RIaQoZgcgCCUCgNA8vyxdFxjJIhKB2GYdCqv1+QKuqWkaDljSoYzJfRkwoJP1+0SpJF2grlCgVtPq0i+vwABlqmSzpAASYB6gTMBkEsAoQXYMZWkHm94ldEAIShMPq1yR4YRhHRwJe5tjEFDyMMfAgFggAGsdcaFRsKfq3XiPV6EtgANQVp4NfwSPT6znVXWUgRytWqlV5fD47q9epNiTSFv6uVmhqByyEEgAdx+B1lCYaDUtKgAKqwoiUiTQITQwIQINIzHQYPG6cv2VyeXyBTiS0ywOLJdLfgB9DCxswADzz0ltoojgIuxfiQv5AkuewLhBF4nOw-hQPUU6znkHbdr2PSwnWDYCIOw4uiQY4Xg0JxUGcvxVBIuyDkBhH+mh+RYvWjbNgR-rzB0pB9n0TCDGgSCfAAbOMkwgLQDBMG6YDkDwfACEIAzjLgYnTEw9wPABhLjEAA)

Pay attention to this piece of code :

```js
function App (state) {
  let view = dom();
    h1("This is a basic demo");
    [...Array(numCounters)].map((_,index) => Counter(state, index));
  udom();
  return view
}
```
We can use plain Javascript in our component, comment it, without
any transpilation, isn't that nice ?