Skip to content

Commit

Permalink
Merge pull request #6 from developit/bug/contstructor-props
Browse files Browse the repository at this point in the history
Pass `props` through Component constructors (fixes #5)
  • Loading branch information
developit committed Dec 3, 2015
2 parents eb0e740 + 26847d4 commit f6ffd61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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--; ) {
Expand All @@ -844,7 +843,7 @@ let componentRecycler = {
}
}
}
return new ctor();
return new ctor(props);
}
};

Expand Down
7 changes: 7 additions & 0 deletions test/browser/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div {...props} />;
}
Expand All @@ -184,6 +189,8 @@ describe('render()', () => {

render(<C2 {...PROPS} />, scratch);

expect(constructorProps).to.deep.equal(PROPS);

expect(C2.prototype.render)
.to.have.been.calledOnce
.and.to.have.been.calledWithMatch(PROPS, {})
Expand Down

0 comments on commit f6ffd61

Please sign in to comment.