项目作者: magic-akari

项目描述 :
A runtime jsx parser. Write your jsx with tagged template literals.
高级语言: TypeScript
项目地址: git://github.com/magic-akari/lit-jsx.git
创建时间: 2018-05-10T07:51:58Z
项目社区:https://github.com/magic-akari/lit-jsx

开源协议:MIT License

下载


lit-jsx · Build Status

A 3kb runtime jsx parser.
Write your jsx with tagged template literals.

lit-jsx is inspired by lit-html and styled-components.

Demo

lit-jsx + React: TodoMVC

Syntax

The syntax of lit-jsx is similar to JSX. Here are differences.

  • You should use ${} to pass value rather than {}.
  • Component should pass in ${}.
  • you should use ...${obj} to pass spread attributes rather than {...obj}.
  • if closing tag is passed by ${}, it will be ignored. </${tag}> is sames as </>.

Example

HelloMessage

  1. // jsx
  2. class HelloMessage extends React.Component {
  3. render() {
  4. return <div>Hello,{this.props.name}</div>;
  5. }
  6. }
  7. ReactDOM.render(<HelloMessage name="Taylor" ></HelloMessage>, mountNode);
  1. // lit-jsx
  2. const jsx = litjsx({ React });
  3. class HelloMessage extends React.Component {
  4. render() {
  5. return jsx`<div>Hello,${this.props.name}</div>`;
  6. }
  7. }
  8. ReactDOM.render(jsx`<${HelloMessage} name="Taylor" />`, mountNode);

Timer

  1. // some editor will recognise html tag function and highlight the code.
  2. const html = litjsx({ React });
  3. class TodoApp extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = { items: [], text: "" };
  7. this.handleChange = this.handleChange.bind(this);
  8. this.handleSubmit = this.handleSubmit.bind(this);
  9. }
  10. render() {
  11. return html`
  12. <div>
  13. <h3>TODO</h3>
  14. <${TodoList} items=${this.state.items} />
  15. <form onSubmit=${this.handleSubmit}>
  16. <label htmlFor="new-todo">
  17. What needs to be done?
  18. </label>
  19. <input
  20. id="new-todo"
  21. onChange=${this.handleChange}
  22. value=${this.state.text}
  23. />
  24. <button>
  25. Add #${this.state.items.length + 1}
  26. </button>
  27. </form>
  28. </div>
  29. `;
  30. }
  31. handleChange(e) {
  32. this.setState({ text: e.target.value });
  33. }
  34. handleSubmit(e) {
  35. e.preventDefault();
  36. if (!this.state.text.length) {
  37. return;
  38. }
  39. const newItem = {
  40. text: this.state.text,
  41. id: Date.now(),
  42. };
  43. this.setState(prevState => ({
  44. items: prevState.items.concat(newItem),
  45. text: "",
  46. }));
  47. }
  48. }
  49. class TodoList extends React.Component {
  50. render() {
  51. return html`
  52. <ul>
  53. ${this.props.items.map(
  54. item => html`<li key=${item.id}>${item.text}</li>`,
  55. )}
  56. </ul>
  57. `;
  58. }
  59. }