Skip to content

<Show keyed> with a function child is not keyed when the function does not have an argument. #1508

Closed

Description

@LiQuidProQuo

Describe the bug

it is expected that when passing keyed property to <Show/>
the component will be keyed, by the value of the when condition.
and that the callback will be called every time the value changes.

however solid correctly "key" a callback only if it has an argument.

I work as expected
https://playground.solidjs.com/anonymous/c1601c39-7e7f-4695-ad4f-6ce46b8bbb41

<Show when={cond()} keyed> 
   { (i_have_an_argument) =><b>{Date.now()}</b> }
</Show>

I don't work 😟
https://playground.solidjs.com/anonymous/c9f3f381-b96c-479b-a1ec-9b43ef3d7338

<Show when={cond()} keyed>
  { ()=><b>{ Date.now() }</b> }
</Show>

Note (root cause):

need to support both a callback and a Signal Getter.
the design decision was, that a callback, is a callback only if it has an argument,
if a function without an argument is provided it is assumed to be a reactive getter / signal.

this may look like magic to the average developer but this is as intended

issue is likely going to be closed as wontfix, and maybe a better handling will come in solid 2.0 which can introduce "breaking change" to fix this.

Your Example Website or App

na

Steps to Reproduce the Bug or Issue

na

Expected behavior

it is expected that in the future solid will find a way that when keyed is provided, it assumes that a callback is provided
when the children is a function, and that the function is not a signal.
and then always call the callback when the key value changes (when={key()})

Screenshots or Videos

No response

Platform

playground

Additional context

No response

Activity

  1. LiQuidProQuo commented on Jan 29, 2023

    @LiQuidProQuo
    ContributorAuthor

    on a related note, until this is fixed / not
    you should always add an argument to a callback. even if it is not needed.

    if you look in this example, <Show> in the top level of a fragment <></> gets re rendered
    when a sibling {count()} is updated.
    https://playground.solidjs.com/anonymous/cc3306cb-156f-480b-bffc-7de037465602

    but if we force it to be a callback by adding an argument we are safe from surprises
    and only the {count()} updates not the random number in the show callback.
    https://playground.solidjs.com/anonymous/80b81f6f-33c9-4469-a84c-b9f16233d1a8

      <>
          <Show when={true} keyed >
            {(add_me_just_in_case_and_you_are_safe) => {
              return <b>{Math.random()}</b>;
            }}
          </Show>
         |  {count()}
        </>
  2. LiQuidProQuo commented on Jan 29, 2023

    @LiQuidProQuo
    ContributorAuthor

    related / duplicate
    #869
    #1149

  3. added
    wontfix
    on Jan 31, 2023
  4. ryansolid commented on Jan 31, 2023

    @ryansolid
    Member

    Yeah I don't believe there is anything to be done about this. I'm not even sure 2.0 has a great solution for this. In hindsight I didn't even need the callback form for this if we had keyed to begin with. Now I know TS is pretty much going to dictate this callback usage unfortunately which means there is no escaping it.

  5. fabiospampinato commented on Jan 31, 2023

    @fabiospampinato

    Why is this not considered a bug if asking for keyed behavior doesn't give you keyed behavior?

  6. ryansolid commented on Jan 31, 2023

    @ryansolid
    Member

    It does give keyed behavior for both scenarios where the API is being used with the parameters. The problem is that it accepts both callback and direct expression form, and in so it is only considered callback if it has an argument.

  7. fabiospampinato commented on Jan 31, 2023

    @fabiospampinato

    If it is only considered a callback if it has an argument isn't that the bug then since callbacks can also have no arguments? Like shouldn't the detection for signals and other things that may be confused with argumentless callbacks be refined instead, perhaps by attaching a symbol (or adding them to some special WeakSet or something else) to things that look like argumentless callbacks to tell them apart?

  8. ryansolid commented on Jan 31, 2023

    @ryansolid
    Member

    It could never be consistent for all cases. This at least can be explained. Consideration around the design of the Show component is less important than implication that Signals can be identified. I could see a future where maybe we could shuffle APIs around or make a different decision here on how top-level expressions could work under this case, but the decision to mark or identify signals is much more fundamental.

  9. fabiospampinato commented on Jan 31, 2023

    @fabiospampinato

    I don't understand why this can't consistently provide the user with keyed behavior, but if can't be fixed there's nothing to do I guess 👍

  10. otonashixav commented on Jan 31, 2023

    @otonashixav
    Contributor

    This (argless keyed Show specifically) doesn't need such a heavy handed approach, cmiiw. For example changing this line to return a referentially different value would fix these issues without causing any problems afaict: https://github.com/solidjs/solid/blob/main/packages/solid/src/render/flow.ts#L113

            return fn ? untrack(() => (child as any)(c as T)) : [child];

    e.g. https://playground.solidjs.com/anonymous/41a887fe-47aa-4cc6-bb72-a8ddafe6eed0

  11. ryansolid commented on Jan 31, 2023

    @ryansolid
    Member

    Oh.. you are suggesting using the fact it is an array which would isolate the change. Interesting. It adds an extra layer on every show component but it would fix this issue.

  12. trusktr commented on Jan 14, 2024

    @trusktr
    Contributor

    I'm in agreement with @fabiospampinato: we should not have differing behavior based on function arity here or in any other places (f.e. in createRoot), because this causes unexpected and unintuitive differences in behavior. It's a best practice to avoid it in general (not just here in Solid).

    If it means we have to place a symbol on a function, or store a function in a map, or something, then that's what we have to do!

    This pattern is not worth any performance gain (in case that's the reason it exists, but I'm not sure).

  13. ryansolid commented on Jan 16, 2024

    @ryansolid
    Member

    @trusktr the symbol on Signals or similar solutions are never adequate. They only ever solve these things in a narrow scope which is no better and is harder to explain. There are places where arity shouldn't be used (like createRoot), and likely ways to avoid it elsewhere. But the symbol is never going to be the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions