skele: Cannot get ref (useRef) for wrapped inside a using functional component
Cannot get ref for <FlatList /> wrapped inside a <Viewport.Tracker></Viewport.Tracker> using functional component
const flatListRef = useRef();
<Viewport.Tracker>
<FlatList
ref={flatListRef}
data={chatMessages}
renderItem={({ item: message }) => renderItem(message)}
keyExtractor={(item) => `${item.id}-message`}
/>
</Viewport.Tracker>
but when using a class component it works fine
<Viewport.Tracker>
<FlatList
ref={(ref) => (this.flatListRef = ref)}
/>
</Viewport.Tracker>
I have tried React.forwardRef but that doesnt seem to work either. As soon as i remove <Viewport.Tracker> it works
Please can you assist?
kind Regards Luke
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 1
- Comments: 15 (2 by maintainers)
Commits related to this issue
- Add section about using ref References #153 — committed to kennym/skele by kennym 4 years ago
- Add section about using ref References #153 — committed to kennym/skele by kennym 4 years ago
I’m having the same issue with functional components.
I was able to get this working with hooks, by using the following syntax:
instead of
🙌
@brax10ward sure here is the entire component snack example
you can just add
and it should work exactly the same
Hey @brax10ward
to answer your question, i am not using createRef(). can you please provide sample code that isnt working, ideally a https://snack.expo.io/ would be awesome?
this should do the trick in a class component.
Thank you too @bantingGamer, I will keep this open still.
Hi @bantingGamer,
To be honest, these components were never tested with hooks. They (still) work using a HoC approach and further more will try to hijack the ref of the inner component for their own use (i.e the HoC are not “clean”).
We’v been thinking about implementing a hooks-based version of these but never had the time. If you like you could attempt to help there with a PR. @bevkoski or @kern3lpan1c might be also interested in helping out
This worked for me
@brax10ward I believe that this is the line where it is created.
ref={(ref) => (this.flatList = ref)}The this keyword is very confusing, I dont fully understand it myself that is why I prefer using functional components. You might want to brush up on how it works