【问题标题】:How to update rendered flat/section list items immediately React Native如何立即更新渲染的平面/部分列表项 React Native
【发布时间】:2020-02-05 11:09:31
【问题描述】:

我正在创建一个ContactListScreenContactListScreen 的直接子级是 ContactItemsContactItems 是一个 sectionList,它呈现每个 ContactItem。 但是问题出现了,因为我的ContactItems 应该是多选的。

我将selectedContacts 的数组从我的状态传递给每个ContactItem。我这里使用的逻辑是ContactItem 检查selectedContacts 的长度是否为0。如果长度为零,它不应该呈现任何selectItemView,如果我选择一个项目,它应该使用回调将自己推送到selectedContacts。但问题是子组件 (ContactItem) 在我选择取消选择项目两次或三次之前不会更新。我怎样才能让它发挥作用?

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>
        );
    }

}

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>
        );
}

注意:我工作的地方不使用功能组件。

【问题讨论】:

  • 请提供一些代码以便更好地理解。
  • 添加了一些代码

标签: javascript reactjs typescript react-native react-state-management


【解决方案1】:

对此我不是 100% 确定,但我感觉问题在于 SectionList 组件没有触发其更新,因为提供的 sections={this.state.data} 属性永远不会改变。

处理此问题的最简单方法是将selectedItems 作为extraData 属性添加到部分列表:

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

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2021-07-07
    • 2017-09-09
    • 2021-09-09
    相关资源
    最近更新 更多