Skip to content

Commit

Permalink
fix solidjs#870: falsey context
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Mar 2, 2022
1 parent 939e87a commit 42c9030
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ export function createContext<T>(defaultValue?: T): Context<T | undefined> {
* @description https://www.solidjs.com/docs/latest/api#usecontext
*/
export function useContext<T>(context: Context<T>): T {
return lookup(Owner, context.id) || context.defaultValue;
let ctx;
return (ctx = lookup(Owner, context.id)) !== undefined ? ctx : context.defaultValue;
}

export type ResolvedJSXElement = Exclude<JSX.Element, JSX.ArrayElement | JSX.FunctionElement>;
Expand Down Expand Up @@ -1577,12 +1578,11 @@ function handleError(err: any) {
}

function lookup(owner: Owner | null, key: symbol | string): any {
return (
owner &&
(owner.context && owner.context[key] !== undefined
return owner
? owner.context && owner.context[key] !== undefined
? owner.context[key]
: owner.owner && lookup(owner.owner, key))
);
: lookup(owner.owner, key)
: undefined;
}

function resolveChildren(children: JSX.Element): ResolvedChildren {
Expand Down
12 changes: 6 additions & 6 deletions packages/solid/src/server/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export function createContext<T>(defaultValue?: T): Context<T> {
}

export function useContext<T>(context: Context<T>): T {
return lookup(Owner, context.id) || context.defaultValue;
let ctx;
return (ctx = lookup(Owner, context.id)) !== undefined ? ctx : context.defaultValue;
}

export function getOwner() {
Expand All @@ -167,12 +168,11 @@ export function runWithOwner(o: Owner, fn: () => any) {
}

export function lookup(owner: Owner | null, key: symbol | string): any {
return (
owner &&
(owner.context && owner.context[key] !== undefined
return owner
? owner.context && owner.context[key] !== undefined
? owner.context[key]
: owner.owner && lookup(owner.owner, key))
);
: lookup(owner.owner, key)
: undefined;
}

function resolveChildren(children: any): unknown {
Expand Down

0 comments on commit 42c9030

Please sign in to comment.