0.0.11 • Published 5 years ago

@xialvjun/react-element v0.0.11

Weekly downloads
20
License
MIT
Repository
github
Last release
5 years ago

react-element

A very simple component, just offering the React.Component API, to write logic just in JSX. With a genc(generator component) helper shamelessly copied and slightly modified from Astrocoders/epitath.

Install

npm i @xialvjun/react-element or yarn add @xialvjun/react-element

Example

import React from "react";
import ReactDOM from "react-dom";
import { Element, init_value, genc, init_state, init_ref, init_refs } from "@xialvjun/react-element";

const App = genc(function*() {
  const [a] = yield <Element construct={init_value("a")} componentDidMount={ele => console.log('didMount', ele.value)} />;
  const [b] = yield <Element construct={init_value("b")} />;
  const [c] = yield <Element construct={init_value("c")} />;
  return (
    <div>
      <button onClick={_ => a.set_value(a.value + "a")}>{a.value}</button>
      <button onClick={_ => b.set_value(b.value + "b")}>{b.value}</button>
      <button onClick={_ => c.set_value(c.value + "c")}>{c.value}</button>
      {/* genc can be used both outside and inside of jsx... but hooks can only be used outside of jsx... */}
      <Element construct={init_value("d")}>
        {genc(function*(ele) {
          const [a] = yield <Element construct={init_value("a")} />;
          const [b] = yield <Element construct={init_value("b")} />;
          const [c] = yield <Element construct={init_value("c")} />;
          return (
            <div>
              <button onClick={_ => a.set_value(a.value + "a")}>
                {a.value}
              </button>
              <button onClick={_ => b.set_value(b.value + "b")}>
                {b.value}
              </button>
              <button onClick={_ => c.set_value(c.value + "c")}>
                {c.value}
              </button>
            </div>
          );
        })}
      </Element>
    </div>
  );
});

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit zl2j7q0r2m

Drawback

The genc will lead to a vdom hell. Well, the source code is plain, but the React Developer Tools will show a vdom hell.

And it's impossible to write a plain version of genc because we need a Lazy component: const Lazy = ({ chidlren }) => chidlren(); to wait for the render-props-component's args. Then one render-props-component needs one deeper level Lazy. It's still vdom hell.

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago