-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a workaround for the potential bug in Show causing double evalu…
…ation of the condition: solidjs/solid#2406
- Loading branch information
Showing
5 changed files
with
50 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import {ChildrenOrFunc, getChildrenElement} from "components/ui/children_func"; | ||
import {createMemo, on} from "solid-js"; | ||
import {Show} from "solid-js"; | ||
|
||
interface Props<T> { | ||
readonly signal: T; | ||
readonly children: ChildrenOrFunc<[T]>; | ||
} | ||
|
||
/** Recreates the children every time the signal is changed. */ | ||
export const Recreator = <T,>(props: Props<T>) => { | ||
const content = createMemo( | ||
on( | ||
() => props.signal, | ||
(signal) => getChildrenElement(props.children, signal), | ||
), | ||
); | ||
return <>{content()}</>; | ||
}; | ||
export const Recreator = <T,>(props: Props<T>) => ( | ||
<Show keyed when={[props.signal] as const}> | ||
{([signal]) => getChildrenElement(props.children, signal)} | ||
</Show> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {ChildrenOrFunc} from "components/ui/children_func"; | ||
import {Accessor, createMemo, JSX, Show} from "solid-js"; | ||
|
||
interface Show_noDoubleEvaluationProps<T> { | ||
readonly when: T; | ||
readonly children: ChildrenOrFunc<[Accessor<NonNullable<T>>]>; | ||
readonly fallback?: JSX.Element; | ||
} | ||
|
||
/** | ||
* A version of <Show> that is not affected by the (potential) bug where the condition is evaluated | ||
* twice at start if the function form is used. See https://github.com/solidjs/solid/issues/2406 | ||
* | ||
* It makes sense to use this replacement if the condition is expensive to evaluate, or if | ||
* the condition creates JSX (e.g. it is of type JSX.Element) to avoid creating two copies of the JSX. | ||
*/ | ||
export const Show_noDoubleEvaluation = <T,>(props: Show_noDoubleEvaluationProps<T>) => { | ||
const when = createMemo(() => props.when); | ||
return <Show {...props} when={when()} />; | ||
}; |