Skip to content

Improvements to JSX #8

@kbairak

Description

@kbairak

I have two proposals to improve JSX functionaity:

  1. Allow saving DOM elements to this. Lets use the word ref for this since it's the closest analogy to React. So this:
class Counter {
  constructor() {
    this.head = (
      <div>
        Count: <span ref="counterSpan">0</span>
        <button onClick={() => this.handleClick()}>Increment</button>
      </div>
    );
  }

  handleClick() {
    const prev = parseInt(this.counterSpan.textContent);
    this.counterSpan.textContent = `${prev + 1}`;
  }
}

Would be equivalent to this:

class Counter {
  constructor() {
    this.head = raw.div(
      raw.text('Count: '),
      this.counterSpan = raw.span(raw.text('0')),
      raw.button(raw.text('Increment', raw.on('click', () => this.handleClick()))),
    );
  }

  handleClick() {
    const prev = parseInt(this.counterSpan.textContent);
    this.counterSpan.textContent = `${prev + 1}`;
  }
}
  1. If the tag's name starts with a capital letter, use a class from the local scope that defines the head attribute. So this:
class App {
  constructor() {
    this.head = (
      <div>
        <h1>My awesome app</h1>
        <Counter />
      </div>
    );
  }
}

Would be equivalent to this:

class App {
  constructor() {
    this.head = raw.div(
      raw.h1(raw.text('My awesome app')),
      (new Counter()).head,
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions