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

Crash while using useAnimatedKeyboard in flat list #5918

Conversation

maciekstosio
Copy link
Contributor

@maciekstosio maciekstosio commented Apr 19, 2024

Summary

Fixes #5822

To keep track of all listeners we use hash map, and notify changes on all of them. When quickly opening flat list with components that use useAnimatedKeyboard and focus on some keyboard, the flat list is still adding new components (adding new listeners), while listeners are notified in the loop about the changes to the keyboard height. Changing HashMap to ConcurrentHashMap seems to fix the issue.

Before After
before.useAnimatedKeyboard.crash.mov
after.useAnimatedKeyboard.crash.mov

Test plan

Test component
import React from 'react';
import {
  FlatList,
  SafeAreaView,
  StyleSheet,
  TextInput,
  View,
} from 'react-native';

import {useAnimatedKeyboard} from 'react-native-reanimated';

function Card() {
  useAnimatedKeyboard();

  return (
    <View
      style={StyleSheet.flatten({
        width: '100%',
        height: 100,
        backgroundColor: 'black',
        marginVertical: 4,
      })}
    />
  );
}
export default function ProfileScreen() {
  const array = Array(1000).fill(0);

  const renderHeader = () => (
    <View>
      <TextInput
        numberOfLines={10}
        placeholder="text input"
        style={StyleSheet.flatten({width: '100%', height: 500, borderWidth: 2})}
      />
    </View>
  );

  return (
    <SafeAreaView style={StyleSheet.flatten({padding: 16})}>
      <FlatList
        keyboardShouldPersistTaps="always"
        data={array}
        renderItem={() => <Card />}
        ListHeaderComponent={renderHeader}
      />
    </SafeAreaView>
  );
}

Copy link

@solitarysp solitarysp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@piaskowyk piaskowyk added this pull request to the merge queue May 1, 2024
Merged via the queue into main with commit 99327e6 May 1, 2024
10 checks passed
@piaskowyk piaskowyk deleted the @maciekstosio/Crash-while-using-useAnimatedKeyboard-in-FlatList branch May 1, 2024 07:52
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

Successfully merging this pull request may close these issues.

ConcurrentModificationException crash on Android
4 participants