-
Notifications
You must be signed in to change notification settings - Fork 282
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
Active Item (UseState) #764
Comments
Your code is wrong. You should follow this advice: const Item = ({title, onPress, isActive, ...props}: ItemProps) => {
const lastItemId = useRef<number>(props.id)
const [active, setActive] = useState<boolean>(isActive);
const tw = useTailwind();
if (lastItemId.current !== props.id) {
lastItemId.current = props.id
setActive(isActive);
}
return (
<TouchableOpacity onPress={onPress}>
<Text style={tw(`py-10 mb-4 ${active ? 'bg-blue-300' : 'bg-red-100'}`)}>
{title}
</Text>
</TouchableOpacity>
);
}; Untested, but it should work. FlashList recycles components, and as a result, it recycles state. To detect if the current item has changed (using the ID as an example), you should store the element ID inside a ref. When you detect that the current ID does not match the last item ID, you should reset every state or set the actual value you need. However, based on your example, you don't need to use useState, as you are already passing isActive as a prop. You could access it directly, and it would work. But since you are likely going to change the state within onPress, my proposed solution would still be valid. If possible, try to avoid using useState as it may decrease performance slightly, but the impact is negligible. |
i'm used it as the documentation and you tell but still not work. data in renderitem component not correct |
This reverts commit 9fe4169. We are temporarily reverting changing to flashlist because it breaks my button for shelving (it doesn't render when clicking on it, you have to scroll down and back up for it to render) and if I have an accordion open and scroll down it opens them on the other audiobooks Try and implement this feature on a branch in the future. Might need to useRef() or something... Shopify/flash-list#764
This doesn't seem to be a bug, for questions you can refer to Github discussions. The @hirbod's suggestion seems to be correct 👍 |
@hirbod This is my code similar to yours and to the docs
I am using react native 0.72.3 and latest stable version of flash-list. Thank you. |
You might need to increase drawDistance to a bigger value (default is 250). |
If anyone else comes across this issue while I did, where you're trying to pipe in data to the E.g.:
|
Good afternoon.
I use FlashList, but it does not work to display the active item in the list.
Here is the code:
Can you advise who faced with this?
I'm following the instructions (https://shopify.github.io/flash-list/docs/fundamentals/performant-components)
Thanks
The text was updated successfully, but these errors were encountered: