Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6211

How to update rendered flat/section list items immediately React Native

$
0
0

I am creating a ContactListScreen. The immediate child of ContactListScreen is ContactItems and ContactItems is a sectionList which renders each ContactItem. But the problem arises, as my ContactItems should be multi-selectable.

I passed the array of selectedContacts from my state to every ContactItem. The logic here I used is ContactItem checks if the length of selectedContacts is 0 or not. If the length is zero it should not render any selectItemView, if I select an item, it should push itself to the selectedContacts using a callback. But the problem is the children components (ContactItem)s doesn't get updated until I selected deselect an item twice or thrice. How can I make it work?

Part of ContactList.tsx

class ContactList extends Component {
    this.state = {
            loading: false,
            error: null,
            data: [],
            selectedItems: []
        };

    handleSelectionPress = (item) => {
        this.setState(prevState => {
            const { selectedItems } = prevState;
            const isSelected = selectedItems.includes(item);
            return {
                selectedItems: isSelected
                    ? selectedItems.filter(title => title !== item)
                    : [item, ...selectedItems],
            };
        });
    };

    renderItem(item: any) {
        return <ContactItem item={item.item}
                isSelected={this.state.selectedItems.includes(item.item)}
                onPress={this.handleSelectionPress}
                selectedItems={this.state.selectedItems}
               />;
    }

    render() {
        return (
            <View style={styles.container}>
                <SectionList
                    sections={this.state.data}
                    keyExtractor={(item, index) => item.id}
                    renderItem={this.renderItem.bind(this)}
                    renderSectionHeader={({section}) => (
                        section.data.length > 0 ?
                            <Text>
                                {section.title}
                            </Text> : (null)
                    )}
                />
            </View>
        );
    }

}

Part of ContactItem.tsx

class ContactItem extend Component {
     render() {
        const checkBox = <TouchableOpacity onPress={() => {
            this.props.onPress(this.props.item)
        }
        } style={this.props.selectedItems.length > 0 && {display: 'none'}}>
            {!this.props.isSelected ?
                <View style={{borderRadius: 10, height: 20, width: 20, borderColor: "#f0f", borderWidth: 1}}>
                </View> : <View style={{
                    borderRadius: 10,
                    height: 20,
                    width: 20,
                    borderColor: "#f0f",
                    borderWidth: 1,
                    backgroundColor: "#f0f"
                }}>
                </View>}

        </TouchableOpacity>
        return (
            <View style={this.styles.contactsContainer}>
                <TouchableOpacity
                    onLongPress={() => this.props.onPress(this.props.item)}>
                    <View style={this.styles.contactInfo}>
                        {checkBox}
                    </View>
                </TouchableOpacity>
            </View>
        );
}

Note: Functional Components are not used where I work.


Viewing all articles
Browse latest Browse all 6211

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>