Skip to content

Commit

Permalink
Merge pull request #111 from developit/component-construction-fixes
Browse files Browse the repository at this point in the history
Support multiple ways to construct components
  • Loading branch information
marvinhagemeister authored Oct 19, 2018
2 parents 093ad23 + 6189843 commit af73d33
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 98 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
4. `setState` no longer modifies `this.state` synchronously
5. Falsy attributes values are no longer removed from the DOM. For some attributes (e.g. `spellcheck`)
the values `false` and `''` have different meaning so being able to render `false` is important
8. The Component constructor no longer initializes state to an empty object. If state has not been
previously set, it will be set to an empty object the first time the component is rendered, after
the constructor has been called

### Minor changes

Expand Down
17 changes: 14 additions & 3 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function Component(props, context) {
this.props = props;
this.context = context;
// if (this.state==null) this.state = {};
this.state = {};
this._dirty = true;
this._renderCallbacks = []; // Only class components
// this.state = {};
// this._dirty = true;
// this._renderCallbacks = []; // Only class components

// Other properties that Component will have set later,
// shown here as commented out for quick reference
Expand Down Expand Up @@ -87,6 +87,17 @@ Component.prototype.forceUpdate = function(callback) {
if (callback!=null) callback();
};

/**
* Accepts `props` and `state`, and returns a new Virtual DOM tree to build.
* Virtual DOM is generally constructed via [JSX](http://jasonformat.com/wtf-is-jsx).
* @param {object} props Props (eg: JSX attributes) received from parent
* element/component
* @param {object} state The component's current state
* @param {object} context Context object, as returned by the nearest
* ancestor's `getChildContext()`
* @returns {import('./index').ComponentChildren | void}
*/
Component.prototype.render = function () {};

/**
* The render queue
Expand Down
2 changes: 2 additions & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ export function diff(dom, parent, newTree, oldTree, context, isSvg, append, exce
c.props = newTree.props;
if (!c.state) c.state = {};
c.context = context;
c._dirty = true;
c._renderCallbacks = [];
}

c._vnode = newTree;
Expand Down
Loading

0 comments on commit af73d33

Please sign in to comment.