Renders its children based on a condition. For the condition to be reactive it needs to be function. The callback will receive a signal for in case it keeps being truthy you can update accordly.
name type description when any once when becomes truthy, it will show its children fallback? any a thing to render as fallback when the condition is falsy
Test using static values
import { render } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { return ( < > < Show when = { true } > Should show this text! </ Show > < Show when = { false } > Nope! </ Show > < span > - the end </ span > </ > ) } render ( Example )
Test using a signal and also the signal value
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) return ( < > < Show when = { showing } > This is reactive </ Show > - { ' ' } < Show when = { showing ( ) } > This is not reactive </ Show > </ > ) } render ( Example )
The signal thats being called wont be reactive because instead of passing to when a function we are passing the signal value which is a boolean How many times a children renders by a toggling signal
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) let rendered = 0 function CountRenders ( ) { rendered ++ return ( < span > This component rendered { rendered } times </ span > ) } return ( < Show when = { showing } > < CountRenders /> </ Show > ) } render ( Example )
Children are unmounted and effectively disposed Using the children helper we can avoid re rendering
import { render , signal , resolve } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) let rendered = 0 function CountRenders ( ) { rendered ++ return ( < span > This component rendered { rendered } times </ span > ) } const Test = resolve ( ( ) => CountRenders ) return ( < Show when = { showing } > < Test /> </ Show > ) } render ( Example )
We use the children helper to memo the result of a component A fallback renders a component when the condition becomes false
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) let rendered = 0 function CountRenders ( ) { rendered ++ return ( < span > This fallback rendered { rendered } times </ span > ) } return ( < Show when = { showing } fallback = { CountRenders } > Hey </ Show > ) } render ( Example )
Fallbacks use memo which are lazy, so these dont need to use the children helper Tests a signal for a fallback.
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) const [ fallback , setFallback ] = signal ( Math . random ( ) ) setInterval ( ( ) => { setFallback ( Math . random ( ) ) } , 1_000 ) return ( < Show when = { showing } fallback = { fallback } > Hey </ Show > ) } render ( Example )
Using the value from the condition in a callback
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing ] = signal ( Math . random ( ) ) setInterval ( ( ) => { setShowing ( Math . random ( ) ) } , 1_000 ) return < Show when = { showing } > { value => value } </ Show > } render ( Example )
Show callbacks receive a read-only signal, a memoChildren are arrays, so the callbacks could be anywhere
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing ] = signal ( Math . random ( ) ) setInterval ( ( ) => { setShowing ( Math . random ( ) ) } , 1_000 ) return ( < Show when = { showing } > < div > The value is: </ div > { value => value } < hr /> { value => value ( ) } < hr /> < div > Is the value above .5? </ div > { value => ( ) => value ( ) > 0 . 5 } < hr /> < div class = " render " > this will blink if its re-rendering </ div > </ Show > ) } render ( Example )
The third callback needs to return a function for it to be reactive Test what happens when a value for a callback becomes null
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { const [ showing , setShowing ] = signal ( { username : Math . random ( ) } ) setInterval ( ( ) => { setShowing ( { username : Math . random ( ) } ) } , 1_000 ) setInterval ( ( ) => { setShowing ( null ) } , 8_000 ) function Test ( props ) { return props . children } return ( < Show when = { showing } > < div > The value is: </ div > { value => ( ) => value ( ) . username } < hr /> { value => < Test > { value ( ) . username } </ Test > } < hr /> < div > Is the value above .5? </ div > { value => ( ) => value ( ) . username > 0 . 5 } < hr /> < div class = " render " > this will blink if its re-rendering </ div > </ Show > ) } render ( Example )
In between 2 elements and in between 2 text nodes
import { render , signal } from ' pota ' import { Show } from ' pota/web ' function Example ( ) { // signal toggles every 1 second const [ showing , setShowing , updateShowing ] = signal ( true ) setInterval ( ( ) => { updateShowing ( showing => ! showing ) } , 1_000 ) return ( < > < span class = " render " > 1 </ span > < Show when = { showing } > < span class = " render " > 2 </ span > </ Show > < span class = " render " > 3 </ span > < hr /> 1 < Show when = { showing } > 2 </ Show > 3 </ > ) } render ( Example )
Sandwich, it keeps the position