Giter Club home page Giter Club logo

gooact's People

Contributors

dependabot[bot] avatar sweetpalma avatar vanhtuan0409 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gooact's Issues

Incremental setState()

The setState() method of Component doesn't allow incremental patches to state - this is a pretty substantial inconsistency from React, and it's a one-line fix.

The other example I posted also illustrates this problem:

https://codesandbox.io/s/rwjr4xo47p

    this.onInput = event => {
      this.setState({
        count: this.state.count, // try commenting out this line
        input: event.target.value
      });
    };

If you don't explicitly copy over all existing state, it fails, whereas React would internally patch state with new properties.

Possible fix: either Object.assign() to existing state property, or spread existing properties to an entirely new value, e.g. {...this.state, new_state} if you're concerned about possible side-effects.

Component.render() is static

So I don't know if you've abandoned this already, but I'm still interested ;-)

To try to make more sense of the types involved, I've started porting it to Typescript.

I've been puzzled by the Component.render method, which is declared as static, gets called as an instance method here, here and here.

I tried this in a console:

class Foo {
  static blah() {
  	console.log('hey');
  }
}

Foo.blah();

(new Foo()).blah(); // blah is not a function

And it fails, as I expected.

It doesn't look like these were introduced by accident? The initial commit declared this as static and then proceeded to call it as an instance-method.

I can't make sense of it. Why would that work?

When i use gooact, I got some problems.

this is my example,this is built in parcel,
and i add "babel-preset-env", and "babel-preset-react",
but when i run this web, i got this error "React is not defined",
I wonder why i need react? Or i need extra config?
(I thounght gooact is a simple react, when i use gooact, i don't need React, is that true?)

Improve loss-of-state, avoid focus work-around

You invited me to open issues (on Medium) so here's the first one :-)

This is regarding the loss-of-focus work-around in the patch function:

        const active = document.activeElement;
        // ...
        [].concat(...vdom.children).map((child, index) => {
            const key = child.props && child.props.key || `__index_${index}`;
            dom.appendChild(pool[key] ? patch(pool[key], child) : render(child, dom));
            delete pool[key];
        });
        // ...
        active.focus();

Here, every child-node is getting repositioned with a call to appendChild - whether the node in question has actually moved or not.

Changing the position of a node in the document causes loss of state, which you can partially work around with activeElement and focus() to restore input focus after losing it - however, there is also loss of selection state and cursor position etc. to consider.

Here's an example to illustrate the problem:

https://codesandbox.io/s/rwjr4xo47p

After typing into the input, try selecting the text you typed in, which is displayed below - you will end up selecting the entire contents of the document, because all the nodes are changing places every second.

Possible approach to fixing this: avoid appendChild and conditionally use insertBefore instead - for example, you could track the previous node by assigning it to a variable, e.g. last_dom = dom, then call dom.insertBefore(new_dom, last_dom.nextSibling) only after checking if (new_dom.previousSibling !== last_dom) ... in plain english, check if the patched/rendered DOM node's previous sibling is already the DOM node from the last iteration, and only position it after the last DOM node if it isn't.

What do you think?

patch bugs

import Gooact, { render, Component } from "../gooact";

class Title extends Component {
  componentDidMount() {
    console.log("title")
    console.log(document.getElementById("title"))
  }

  render() {
    return (
      <h1 id="title">{this.props.children}</h1>
    )
  }
}

class App extends Component {
  constructor(props) {
    super(props);
    this.state = { arr: [{id: 1,v:1},{id:2, v:2}] };

    this.onReverse = this.onReverse.bind(this);
  }

  componentDidMount() {
    console.log("app");
  }

  onReverse() {
    this.setState({arr: [{id: 2,v:2},{id:1, v:1}]})
  }

  render() {
    const { counter } = this.state;
    return (
      <div>
        {
          this.state.arr.map(o=><div key={o.id}>{o.v}</div>)
        }
        <button onClick={this.onReverse}>reverse</button>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

when click, it render
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.