Skip to content

Commit

Permalink
fix context inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
huang2002 committed Jan 21, 2019
1 parent b6cb944 commit 3ca9229
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/core/HNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const toNodeArr = function toNodes(

src.ownerNode = ownerNode;
src.owner = owner;
src.context = context;

if (desc) {

Expand All @@ -80,7 +81,7 @@ export const toNodeArr = function toNodes(

src.active = false;

initComponent(src, store, context);
initComponent(src, store);

return src.nodes = toNodeArr(
src.output = toArr(
Expand Down
9 changes: 4 additions & 5 deletions src/core/initComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { HNode } from "./HNode";
import { Store } from "./Store";
import { supply } from "../utils/helpers";

export const initComponent = function (hNode: HNode<any>, store: Store, ctxStore: Store) {
export const initComponent = function (hNode: HNode<any>, store: Store) {

const { props } = hNode,
const { props, context: ctxStore } = hNode,
desc = hNode.desc!,
{ defaultProps, defaultStore, storeHandlers, state, context, init } = desc;

Expand All @@ -23,13 +23,12 @@ export const initComponent = function (hNode: HNode<any>, store: Store, ctxStore
store.handleSome(storeHandlers);
}

hNode.context = ctxStore;
if (context) {
ctxStore.bind(hNode, context);
ctxStore!.bind(hNode, context);
}

if (init) {
init.call(hNode, props, store, ctxStore);
init.call(hNode, props, store, ctxStore!);
}

};
2 changes: 1 addition & 1 deletion src/ticker/updateChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const updateChildren = function (
renderToDOM(newChildren.slice(oldChildrenCount), {
parent: element,
owner: hNode,
context: hNode.context!
context: hNode.context
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/src/Greeting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Greeting = HUI.define<GreetingProps, {}, TestContext, {}, TestContextHandl
const ShowTarget = HUI.define<{}, {}, TestContext>('ShowTarget', {
context: ['target'],
render(props, store, context) {
return <p>context.target: {context.get('target')}</p>;
return `context.target: ${context.get('target')}`;
}
});

Expand Down
4 changes: 3 additions & 1 deletion test/src/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ HUI.render<TestContext, TestContextHandlers>(

<hr />

<ShowTarget />
<p>
<ShowTarget />
</p>

<hr />

Expand Down

0 comments on commit 3ca9229

Please sign in to comment.