简体   繁体   中英

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

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.

I'm not 100% certain about this, but I have a feeling that the problem is that the SectionList component isn't triggering its update because the supplied sections={this.state.data} property never changes.

The easiest way to handle this is to add the selectedItems as an extraData property to section list:

 <SectionList
     sections={this.state.data}
     extraData={this.state.selectedItems}
     //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM