Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keys implementation? #9

Open
brainkim opened this issue Sep 5, 2019 · 6 comments
Open

keys implementation? #9

brainkim opened this issue Sep 5, 2019 · 6 comments

Comments

@brainkim
Copy link

brainkim commented Sep 5, 2019

Hi. I’m wondering if you considered implementing the react key prop? Wondering what that looks like under the hood and haven’t found an approachable resource for it yet.

Thanks for the blog posts/implementation!

@pomber
Copy link
Owner

pomber commented Sep 5, 2019

Hey, I haven't. Let me know if you find something or if you try to do it yourself, I'd like to add a link to it.

@yisar
Copy link

yisar commented Jan 4, 2020

I implemented this in fre. It has the same performance as react, but it's super simple.
https://github.com/yisar/fre/blob/master/src/reconciler.js#L270

@brainkim
Copy link
Author

brainkim commented Jan 4, 2020

@yisar can you explain the general algorithm you use? I can’t seem to figure out from the code itself sorry :)

@yisar
Copy link

yisar commented Jan 5, 2020

@brainkim Well, I can explain it, it is similar to React.

A B => B A
  1. insertPoint
    first, save the insertPoint(Usually the previous fiber), when traversing B, its insertPoint is A, and insertPoint of A is null
  2. insertBefore
    If there is insertPoint, it will insert before it, so B will insert before A
    if insertPoint is null, it will insert before null, same to appendChild
  3. reused
    Only reused fiber have insertPoint, others have not
    if there is no insertPoint, it will insert befroe first child
  4. key and index
    React use not only key but also index to reused fibers, I simplify this.
    I use hash to mark them meanwhile.

Generally speaking, it is actually the processing of insertpoint and reuse. In React, it distinguishes a variety of situations, and Fre super simplifies it.

You can try to debug them by yourself!

@Combo819
Copy link

Hi @yisar Did you change the implementation of reconciliation in fre? I'm confused after reading the explanation and the code. Thanks

@yisar
Copy link

yisar commented Apr 13, 2021

@Combo819 In Fre2 version, I refactor the reconciliation algorithm. The new algorithm uses Collision pointer for preprocessing, which is better implemented and easier to understand.

I can reinterpret the new algorithm implementation in Fre2:

1 2 3 4
1 3 2 4

We traverse from both ends to the middle, and find that 1 and 4 are exactly the same, then we skip them, and then it becomes:

2 3
3 2

The remaining nodes can be reused. We save their DOM references. Finally, we just need to change the position, that is, 3 is inserted in front of 2.

Of course, if you are interested in the old algorithm, you can look up the code of fre1, but I think fre2 will be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants