You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Changing onViewableItemsChanged on the fly is not supported #30171
Reacted by Blanc Adrien, rcasari1987, Vitaliy, Dipesh Wagle, Tristan Debroise, John Haupenthal, Shahbaaz Ahmad, RedkoVO, Andrey Ramirez, uzaysan and 60 more
Reacted by drop90prog, showtan001, a-mar-se and fabien-lenoir-idtgv
Reacted by switchzone, ⛥ 𝕲𝖍𝖔𝖘𝖙𝖌𝖑𝖎𝖙𝖈𝖍 ☯ ∞Hz, Tristan Debroise, amadeirado, Asim Khatri, Priya Rajput, Jenipha Rachel, Brad Jones, Alejandro Molina, Lucas Leal and 10 more
Reacted by Yeabsira Gashaw, Asim Khatri, Dhaniel Sales, Priya Rajput, FadiAboMsalam, Alejandro Molina, eric-delguerra, William N., Anubhav Sharma, Chanchal Kumar Mishra and 4 more
Reacted by Sobhan Bera, amadeirado, Asim Khatri, Priya Rajput, eric-delguerra, Lucas Leal, Chanchal Kumar Mishra, Grant Bartone, Nardi-Liantsoa, RayKay91 and 5 more
Reacted by Ajayi Thadeus, Henrik Hansen, Priya Rajput, Thales Ludwig, Matija Grcic, Dina, Jenipha Rachel, Emil Cieslar, Manuela Viola, drop90prog and 45 more
Reacted by Antsthebul, yash malawant and Nhan Doan
Reacted by FadiAboMsalam, Nadjem eddine, Max, Abdelmonaime Chourafi, Alex Lee, Manil@SoftNEP, VanDinh18, Peniel Nyinaku, bruteforks, StarCaptain17 and 5 more
Reacted by Abdelmonaime Chourafi, Cleyson Barbosa, Alex Lee, Manil@SoftNEP, Peniel Nyinaku, bruteforks, Pedro Borges, yash malawant and Nhan Doan
tried viewabilityConfigCallbackPairs in the function component, But getting Changing onViewableItemsChanged on the fly is not supported Error and Invariant Violation: Changing onViewableItemsChanged on the fly is not supported. Below is my code
@isnifer tried viewabilityConfigCallbackPairs in the function component, that error got resolved but in the onViewableItemsChanged function I'm using a prop whose value is getting changed but inside the function, I'm not getting that prop new value, it's always giving me the same value which is coming for it the first time
const indexOfHighlightedProduct = viewableItems.findIndex(({ item }) => item?.styleId === inFocusProduct)
console.log('inFocusProduct', inFocusProduct)
// if it is currently visible don't animate else animate
if (indexOfHighlightedProduct > -1) {
console.log('don"t do something')
} else {
console.log('do something')
}
}
Here inFocusProduct is the prop whose value is getting but it's not showing the changed value
If we are using onViewableItemsChanged with useRef or useCallback then we are not getting updated redux values directly in onViewableItemsChanged. We need to use useRef with redux values as well. Is there any other approach for the same?
If we pass any dependency in useCallback (with onViewableItemsChanged) then it gives same error Changing onViewableItemsChanged on the fly is not supported
I'm not sure what the point of having a functioned defined in stone (useRef, useCallback with []) to receive the viewable item updates when it can't DO anything with that information - cannot update state or anything like that... it's pointless.
I tagged this as a bug but not sure when the RN team will be able to dedicate time to it.
In the meantime, a workaround like this should work:
import{useCallback,useInsertionEffect,useRef}from'react'functionMyComponent(){constonViewableItemsChanged=useNonReactiveCallback(()=>{// ... your code ...});<FlatListonViewableItemsChanged={onViewableItemsChanged}// .../>}// Note: Use this sparingly.functionuseNonReactiveCallback<TextendsFunction>(fn: T): T{constref=useRef(fn)useInsertionEffect(()=>{ref.current=fn},[fn])returnuseCallback((...args: any)=>{constlatestFn=ref.currentreturnlatestFn(...args)},[ref],)asunknownasT}
Reacted by Naoki Otsu, nico-locket, Dmytro Lebedynskyi, Khue Pham and Ahmed Mahmoud
Depending on what you need to update in your onViewableItemsChanged, it is possible to create some useState to track the index, then a useEffect with that index as dependency. This effect will have access to the whole scope of the functional component and be able to make any change and read fresh values.
ex:
constmyComponent=()=>{const[items,setItems]=useState();useEffect(()=>{// execute some code when items change wich involves reading/changing component state},[items])constupdateItems=useCallback(({ViewableItems})=>{setItems(ViewableItems);},[])// lack of dependencies will avoid triggering the errorreturn(<FlatlistonViewableItemsChanged={updateItems}.../>}
Activity
isnifer commentedon Oct 18, 2020
@zerocsss I recommend you to use this technique:
First of all change property to pass this callback
where
viewabilityConfigCallbackPairsis:That's it.
adblanc commentedon Nov 12, 2020
Thanks it works like a charm
arbaz-yousuf-jazsoft commentedon Dec 23, 2020
@zerocsss could you please explain it, It didn't work for me
rajeshivn commentedon Mar 29, 2021
same here not working
Nikolay-Vovk-dataart commentedon Apr 9, 2021
you also could wrap it with
useCallbackif it hasn't any deps (useCallback(() => {...} , []))johnhaup commentedon Apr 16, 2021
@arbaz-yousuf-jazsoft @rajeshivn or anyone still trying to implement the solution above:
Add this to the top of your functional component:
then render your Flatlist:
Thanks @isnifer this worked great!
omitchen commentedon May 12, 2021
This method still has no response in my project. Although no error is reported, onViewableItemsChanged will not be executed.
julienripet commentedon Jun 23, 2021
@omitchen I had the same problem, my issue was that I had changed the name of the function
onViewableItemsChangedinIf you want to use a function with a different name, make sure to set it as the
onViewableItemsChangedproperty in the useRef, as followssobhanbera commentedon Aug 6, 2021
This worked for me. Thanks!!
sallarahmed commentedon Oct 4, 2021
Convert component from functional to class and call these methods, this will work like a charm
Qazafi-Dev commentedon Nov 24, 2021
@johnhaup i doing same thing in class component but it giving me error can't find variable onviewablechangeitem but i defined it
Qazafi-Dev commentedon Nov 24, 2021
@sallarahmed can u share class component code? i need it
prasanthvenkatachalam commentedon Dec 10, 2021
tried viewabilityConfigCallbackPairs in the function component, But getting Changing onViewableItemsChanged on the fly is not supported Error and Invariant Violation: Changing onViewableItemsChanged on the fly is not supported. Below is my code
Also tried the below code, this does not throw any error but function call is not happening. Help needed
const viewabilityConfigCallbackPairs = useRef([
{ viewabilityConfig, onViewableItemsChanged: handleChangeVIew },
]);
abhilakshyadobhal commentedon Jun 1, 2022
@isnifer tried viewabilityConfigCallbackPairs in the function component, that error got resolved but in the
onViewableItemsChangedfunction I'm using a prop whose value is getting changed but inside the function, I'm not getting that prop new value, it's always giving me the same value which is coming for it the first timeHere
inFocusProductis the prop whose value is getting but it's not showing the changed value22 remaining items
AnandKuhar1100 commentedon Jul 16, 2023
If we are using onViewableItemsChanged with useRef or useCallback then we are not getting updated redux values directly in onViewableItemsChanged. We need to use useRef with redux values as well. Is there any other approach for the same?
Changing onViewableItemsChanged on the fly is not supportedKhineKhineMyatNoe commentedon Aug 23, 2023
this one works for react native 0.71.1
SirCameron commentedon Oct 6, 2023
I'm not sure what the point of having a functioned defined in stone (useRef, useCallback with []) to receive the viewable item updates when it can't DO anything with that information - cannot update state or anything like that... it's pointless.
nguyentrancong commentedon Jan 1, 2024
I've fixed it, and working with me,
You see code belw
` const viewConfigRef = React.useRef({viewAreaCoveragePercentThreshold: 50});
const onViewCallBack = React.useCallback(async (info: any) => {
const item = await head(info?.viewableItems);
onChangePosition(item.index);
}, []);
return (
<FlatList
ref={flatListRef}
data={questions}
keyExtractor={item => item.id}
renderItem={renderItemQuestion}
style={styles.flatList}
pagingEnabled
horizontal
showsHorizontalScrollIndicator={false}
viewabilityConfig={viewConfigRef.current}
onViewableItemsChanged={onViewCallBack}
/>
);`
gaearon commentedon Mar 14, 2024
I tagged this as a bug but not sure when the RN team will be able to dedicate time to it.
In the meantime, a workaround like this should work:
NickGerleman commentedon Mar 14, 2024
RN 0.73 makes this a bit better: 5cfa125
imfunniee commentedon Mar 18, 2024
hopefully the new architecture solves this
Frankcaozas commentedon Apr 27, 2025
if i am using react 16, can i replace useInsertionEffect with useLayoutEffect
mchaptel commentedon Feb 28, 2026
Depending on what you need to update in your onViewableItemsChanged, it is possible to create some useState to track the index, then a useEffect with that index as dependency. This effect will have access to the whole scope of the functional component and be able to make any change and read fresh values.
ex: