【问题标题】:react-native , customer flatlist picker, returning previous values on selectionreact-native ,客户平面列表选择器,在选择时返回以前的值
【发布时间】:2021-05-04 00:32:21
【问题描述】:

下面是客户选择器的代码组件

import React, { useEffect, useState } from "react";
import { connect } from 'react-redux';
import {
    TouchableOpacity,
    FlatList,
    SafeAreaView,
    StatusBar,
    StyleSheet,
    Text,
    View,
    Button,
    Alert,
} from "react-native";
import { screenHeight, screenWidth } from "../constants";

const DATA = [
    {
        id: "bd7acbea-c1b1-46c2-aed5-3ad53abb28ba",
        title: "Client Not Found"
    },
    {
        id: "58694a0f-3da1-471f-bd96-145571e29d72",
        title: "Client refused"
    },

];

const Item = ({ item, onPress, style }) => (
    <TouchableOpacity onPress={onPress} style={[styles.item, style]}>
        <Text style={styles.title}>{item.title}</Text>
    </TouchableOpacity>
);


const StatusOptions = (props) => {

    const [selectedId, setSelectedId] = useState(null);
    const renderSeparator = () => (
        <View
            style={{
                backgroundColor: "grey",
                height: 0.8
            }}
        />
    );
    const ListHeader = () => {
        //View to set in Header
        return (
            <View style={{ height: 20 }}></View>
        );
    };

    
    const renderItem = ({ item }) => {
        const backgroundColor = item.id === selectedId ? "#6cd9ff" : "white";

        return (
            <Item
                item={item}
                onPress={() => {
                    setSelectedId(item.id);
                    console.log("SELECTED ID _ STATUSOPTIONS component : ", selectedId);

                    const val = DATA.filter(status => status.id == selectedId).map(filteredStatus => filteredStatus.title);

                    console.log("VALLLLLLLLLLLLLLLLLLLLLLLUUUUUUEEEEEEEEEEEEEEEEEEEE:::: ", val);

         props.navigation.navigate('AnotherScreen');          
                }}
                style={{ backgroundColor }}
            />
        );
    };

    return (

        <View style={{ bottom: 0, flex: 1, position: 'absolute', width: screenWidth, }}>
            <View style={styles.container}>
                <FlatList
                    data={DATA}
                    renderItem={renderItem}
                    keyExtractor={(item) => item.id}
                    extraData={selectedId}
                    ItemSeparatorComponent={renderSeparator}
                    ListHeaderComponent={ListHeader}
                    style={{
                        backgroundColor: "white",
                        width: "100%",
                        borderTopRightRadius: 20,
                        borderTopLeftRadius: 20,
                        zIndex: 1,
                    }}
                />
            </View>

            <View style={{ backgroundColor: "grey", height: 0.4 }} />

            <View style={styles.closeButtonContainer}>
                <TouchableOpacity style={styles.closeButton}
                    onPress={() => {
                        props.setStatusOptionsVisible(false)
                    }}>
                    <Text style={styles.title}>Close</Text>
                </TouchableOpacity>
            </View>
        </View>
    );
};

function mapStateToProps(state) {
    return {
        StatusOptionsVisible: state.volunteerItems.statusOptionsVisible,
        currentTaskItemId: state.taskItems.taskItemId,
    };
}

function mapDispatchToProps(dispatch) {
    return {
        setStatusOptionsVisible: (visible) => dispatch({ type: 'SET_STATUS_VISIBLE', statusVisibility: visible }),

    };
}

export default connect(mapStateToProps, mapDispatchToProps)(StatusOptions);

const styles = StyleSheet.create({
    closeButton: {
        backgroundColor: 'lightgrey',
        borderRadius: 10,
        height: 50,
        justifyContent: 'center',
        width: '90%',
    },
    closeButtonContainer: {
        alignItems: 'center',
        height: 90,
        justifyContent: 'center',
        backgroundColor: 'white',
    },
    textStyle: {
        textAlign: "center",
    },
    container: {
        borderRadius: 30,
        flex: 4,
        width: screenWidth,
    },
    item: {
        padding: 20
    },
    title: {
        fontSize: 20,
        fontWeight: 'bold',
        color: "black",
        textAlign: "center"
    }
});

控制台日志:(“SELECTED ID _ STATUSOPTIONS 组件:”,selectedId) 在 render Item 函数中为第一个选择器项目选择返回 null 并为下一个选择器项目选择返回前一个值,任何人都可以帮忙修复它吗?

【问题讨论】:

  • 使用 setState 后的值是之前的,所以你需要使用 useEffect 来读取更新的值。或者您可以在 onPress item.id 中使用而不是 selectedId。更多信息在这里stackoverflow.com/questions/54119678/is-usestate-synchronous
  • 非常感谢!!,这确实有效,我没想过只打印 item.id 并且一直在尝试打印状态值 -_-。
  • 在从选择器获取值后,我试图导航到另一个屏幕,但它失败并出现异常:错误类型错误:未定义不是对象(评估'props.navigation.navigate'),任何知道为什么会这样,尽管在道具中我确实看到了导航数据
  • 试试这个: const navigation = useNavigation();在usestate之后写这个

标签: javascript reactjs react-native react-hooks picker


【解决方案1】:

尝试使用这个

 useEffect(() => {
          console.log("SELECTED ID _ STATUSOPTIONS component : ", selectedId);
          if(selectedId != null) {
          const val = DATA.filter(status => status.id == selectedId).map(filteredStatus => filteredStatus.title);
   
         console.log("VALLUUEEEEEEEEEEEEEEEEEEEE:::: ", val);
}
        }, [selectedId])

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2022-11-21
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多