DEV Community

Cover image for Linked lists in the wild: React Hooks

Linked lists in the wild: React Hooks

Conlin Durbin on December 17, 2018

I was digging into some of the code for React hooks the other day, right after reading Ali's great post on linked lists: ...
Collapse
 
dance2die profile image
Sung M. Kim

Seeing (what I thought only academic in nature) Linked List, used in React sounds great. It definitely helps on understanding "how" the fiber was implemented (as you also mentioned "why" in the Fiber documentation link).

I've also run into another place in React source, where they heavily utilizes Linked List.

React-cache is a reference implementation of upcoming data fetch feature.
The underlying implementation uses LRU (Least Recently Used) cache, which uses linked list to track cached resources.

Collapse
 
wuz profile image
Conlin Durbin

Ooh! That one is a circular doubly-linked list, which is really cool!

Collapse
 
dance2die profile image
Sung M. Kim

I honestly still haven't been able to figure the whole code out yet 😅

Thread Thread
 
wuz profile image
Conlin Durbin

Maybe I can do another one of these! I haven't read it yet though! Looks complicated!

Thread Thread
 
dance2die profile image
Sung M. Kim

Looking forward to your next post 🙂. Thank you :p

Collapse
 
madhuvanth_g profile image
Madhuvanth Gopalan

Thanks for this post, Conlin. I've a question, maybe a naive one. Am trying to reason about why Hooks need a Linked list implementation. One usecase (useCase? ;)) it definitely seems to facilitate is, share state of one Hook to another. Curious to hear your thought process on this.

Collapse
 
madhuvanth_g profile image
Madhuvanth Gopalan

Maybe also to run all the effects one after another.

Collapse
 
madhuvanth_g profile image
Madhuvanth Gopalan

Ignore this naive question. I think, I figured out. Linked List allows React to rely on the order of Hooks calls which means, it could preserve state between re-renders.

Collapse
 
tonicla53091281 profile image
toniclark

I think this attribute can be tried to be learnt as first learner of programming. However, what should I do to start it ? Should I follow and try to understand the code or what ?

Collapse
 
wuz profile image
Conlin Durbin

Definitely check out Ali's post on Linked Lists: dev.to/aspittel/thank-u-next-an-in...

Hopefully this article isn't too difficult if you have some understanding of Javascript. I'd be happy to explain anything you find confusing!

Collapse
 
aspittel profile image
Ali Spittel

Woah, this is so cool!

Collapse
 
dangolant profile image
Daniel Golant

Great breakdown, Conlin!

Collapse
 
wuz profile image
Conlin Durbin

Thanks Dan :D

Collapse
 
paddy3118 profile image
Paddy3118

What language is that? Doesn't it have a list type with append, delete, and insert methodss

You don't wwant to have to mess with null pointers and memory allocation, unless you really need to.