Skip to content

Instantly share code, notes, and snippets.

@clemmy
Last active January 23, 2025 01:09
Show Gist options
  • Save clemmy/b3ef00f9507909429d8aa0d3ee4f986b to your computer and use it in GitHub Desktop.
Save clemmy/b3ef00f9507909429d8aa0d3ee4f986b to your computer and use it in GitHub Desktop.
Render 1 Render 2 State Preserved?
<>...</> <>{[...]}</> yes (in any level)
<><>...</></> <>...</> no
[...] [[...]] no
[<>...</>]* [...] no
[<>...</>]* <>...</> no
[<>...</>]* <>[...]</> no
[<>...</>]* [[...]] yes
[<>...</>]* <><>...</></> yes
<>{[...]}</> <><>...</></> no
<>{[...]}</> [[...]] no
<><>...</></> [[...]] yes (only when nested two levels from top)
<><>...</></> <><>...</></> yes
[[...]] [[...]] yes
[...] <>...</> yes (top level only)
... <>...</> yes (top level only)
... [...] yes (top level only)
  • Render warns since the fragment is unkeyed. We'll treat it as undefined behavior.

Note: The state preservation behavior is commutative of the renders; It doesn't matter whether render 1 or 2 happens first, the state preservation behavior should be the same.

@clemmy
Copy link
Author

clemmy commented Oct 23, 2017

I created a few permutations of different ways of using children, array, and fragment syntax in React (not mutually exclusive), trying to understand the desired behavior a bit better.

@WinnyBunny
Copy link

WinnyBunny commented Aug 30, 2024

Interesting 🤔

@asifurrahaman754
Copy link

asifurrahaman754 commented Dec 24, 2024

I am not sure if the last two row condition is correct, I tried like this:

{isFancy ? ( <Counter isFancy={true} /> ) : ( <><Counter isFancy={false} /></> )}

according to the docs they should preserve state, but it is not

@sadif50
Copy link

sadif50 commented Dec 24, 2024

@asifurrahaman754 you can try this:

return (
    isFancy ? (
      <Counter isFancy={true} />
    ) : (
      <>
        <Counter isFancy={false} />
      </>
    )
  );

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