From 43f696a7b684dec9b92ef9fed309ba5a94cac864 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Thu, 3 Dec 2015 14:13:23 -0500 Subject: [PATCH 1/2] Pass `props` through Component constructors --- src/preact.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/preact.js b/src/preact.js index 927662e842..391a21cca2 100644 --- a/src/preact.js +++ b/src/preact.js @@ -410,7 +410,7 @@ function renderComponent(component, opts) { setComponentProps(inst, childProps, SYNC_RENDER); } else { - inst = componentRecycler.create(childComponent); + inst = componentRecycler.create(childComponent, childProps); inst._parentComponent = component; component._component = inst; if (component.base) deepHook(inst, 'componentWillMount'); @@ -491,9 +491,8 @@ function buildComponentFromVNode(dom, vnode) { * @private */ function createComponentFromVNode(vnode) { - let component = componentRecycler.create(vnode.nodeName); - let props = getNodeProps(vnode); + let component = componentRecycler.create(vnode.nodeName, props); setComponentProps(component, props, NO_RENDER); renderComponent(component, DOM_RENDER); @@ -835,7 +834,7 @@ let componentRecycler = { else componentRecycler.components[name] = [component]; }, - create(ctor) { + create(ctor, props) { let list = componentRecycler.components[ctor.name]; if (list && list.length) { for (let i=list.length; i--; ) { @@ -844,7 +843,7 @@ let componentRecycler = { } } } - return new ctor(); + return new ctor(props); } }; From 26847d46fd602aa486c2b365841c6778c92047f0 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Thu, 3 Dec 2015 14:13:37 -0500 Subject: [PATCH 2/2] Basic test for constructor props --- test/browser/render.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/browser/render.js b/test/browser/render.js index 15f6009912..65a70de193 100644 --- a/test/browser/render.js +++ b/test/browser/render.js @@ -174,8 +174,13 @@ describe('render()', () => { it('should render components with props', () => { const PROPS = { foo:'bar', onBaz:()=>{} }; + let constructorProps; class C2 extends Component { + constructor(props) { + super(props); + constructorProps = props; + } render(props) { return
; } @@ -184,6 +189,8 @@ describe('render()', () => { render(, scratch); + expect(constructorProps).to.deep.equal(PROPS); + expect(C2.prototype.render) .to.have.been.calledOnce .and.to.have.been.calledWithMatch(PROPS, {})