导航回屏幕时,React本机元素Button Group按钮消失

如何解决导航回屏幕时,React本机元素Button Group按钮消失

我一直试图找出我在这里做错了什么,但我做不到。我有两个屏幕,分别从数据库提取数据,然后单击一个项目(类别)。然后,当按下某个单词时,该项目(类别)会从我的数据库提取更多数据。我必须在Viewcategory屏幕上为用户提供两个选择。他们可以按向后箭头以返回并从其他类别中选择更多项目,或者可以单击下一步按钮将其带到另一个屏幕。我遇到的问题是当我按后退箭头,然后返回以选择另一个类别时。选择该类别后,下一个按钮消失。除非我重新加载整个项目,否则它不会重新出现。

这是菜单WS的第一个屏幕

constructor(props) {
        super(props);
        this.state = {
            restaurantlocationcode: '',dataSource: [],}
        this.updateIndex = this.updateIndex.bind(this)
    }

    updateIndex (selectedindex) {
        this.setState({selectedindex})
        this.viewMenu(selectedindex)
    }

    render() {

        const buttons = ['Menu']
        const { selectedindex } = this.state

        return (
            <View style={styles.container}>
                <MenuButtonWS navigation={this.props.navigation} />

                <Input
                    style={styles.Input} 
                    placeholder='Restaurant Location Code'
                    leftIcon={
                        <Icon
                        name='location-arrow' 
                        size={24}
                        color='black'
                        />
                    }
                    onChangeText={ (restaurantlocationcode) => this.setState({restaurantlocationcode})}
                    value={this.state.restaurantlocationcode}
                    underlineColorAndroid='transparent'
                />

                <ButtonGroup
                onPress={this.updateIndex}
                selectedindex={selectedindex}
                selectedButtonStyle={{backgroundColor: 'blue'}}
                buttons={buttons}
                containerStyle={styles.buttonGroup} 
                />

                <FlatList 
                    data={this.state.dataSource}
                    exTradata={this.state}
                    keyExtractor={(item,index) => index.toString()}
                    renderItem={({item,index}) => (
                        <ListItem
                        titleStyle={{ color: 'black',fontWeight: 'bold'}}
                        title={`${item}`}
                        onPress={() => this.viewCategory(item)}
                        bottomDivider
                        chevron
                        />
                    )}
                />

            </View>
        )
    }

    viewMenu = (index) => {
        if (index === 0) {
            let rlc = {
                restaurantlocationcode: this.state.restaurantlocationcode,};
            
            fetch('URL',{
                method: 'POST',body: JSON.stringify(rlc),headers: {
                    'Accept': 'application/json','Content-Type': 'application/json',},})
                .then((response) => response.json())
                .then((responseJson) => {
                    this.setState({
                        dataSource: responseJson,});
                    console.log(responseJson)
                    
                })
                .catch((error) => {
                    console.log(error);
                });
        }
    }
    
    viewCategory = (item) => {
        
        fetch('URL',{
            method: 'POST',body: JSON.stringify({
                category: item,restaurantlocationcode: this.state.restaurantlocationcode,}),headers: {
                'Accept': 'application/json',})
            .then((response) => response.text())
            .then((responseJson) => {
                this.setState({
                    dataSource: responseJson,});
                console.log(responseJson)
                
                this.setState({dataSource: []})
                this.props.navigation.navigate('ViewCategoryWS',{
                    food: responseJson,otherParam: '101',});
            })
            .catch((error) => {
                console.log(error);
            });
    }
}

在此屏幕上您可以查看每个类别中的项目(“查看类别”屏幕)

constructor(props) {
        super(props);
        const  { navigation } = this.props;
        const cust = navigation.getParam('food','No-User');
        const other_param = navigation.getParam('otherParam','No-User');
        const cust1 = JSON.parse(cust);
        const data = cust1.map(item => ({label: item,checked: false}));
        this.state = {
            dataSource: [],data,checked: null,selectedindex: 2,}
        this.updateIndex = this.updateIndex.bind(this)
    }

    updateIndex (selectedindex) {
        this.setState({selectedindex})
        this.nextScreen(selectedindex)
    }

    render() {
        const buttons = ['Next']
        const { selectedindex } = this.state
        const {data} = this.state;
        console.log(data);

        return (
            <View style={styles.container}>
                <BackButtonWS2 navigation={this.props.navigation} />
                
                <FlatList
                    data={data}
                    exTradata={this.state}
                    keyExtractor={(item,index) => index.toString()}
                    renderItem={({ item,index }) => (
                        <CheckBox 
                        center 
                        titleProps={{ color: 'black',fontWeight: 'bold'}}
                        title={item.label}
                        iconRight
                        checked={item.checked}
                        size={30}
                        onPress={() => this.onCheck(index,item.label)}
                        containerStyle={styles.checkBox} 
                        />    
                    )}
                />
                <ButtonGroup 
                onPress={this.updateIndex}
                selectedindex={selectedindex}
                selectedButtonStyle={{backgroundColor: 'blue'}}
                buttons={buttons}
                containerStyle={styles.buttonGroup}
                />
                

            </View>
        )
    }

    onCheck = (index,item) => {
        let {data} = this.state;
        data[index].checked = !data[index].checked;
        this.setState({data})
        this.state.dataSource.push([item]);
        //alert(this.state.dataSource);
        console.log(this.state.dataSource);
    }

    nextScreen = (index,item) => {
        if (index === 0) {
            this.props.navigation.navigate('UsersBill',{
                foodCat: this.state.dataSource,});
            alert(this.state.dataSource);   
        }
    }
}

这是我的后退按钮代码

  _back = () => {
        this.props.navigation.navigate('MenuWS',{
            food: 'food',});
    } 

    render() {
        return(
            <Icon 
                name='arrow-left'
                color="#000000"
                size={25}
                style={styles.menuIcon}
                onPress={() => this._back()}
            />
        )
    }
}

我尝试使用goBack函数,但这只是将我带回到主屏幕,而不是我需要的屏幕。我还尝试将我选择的参数发送回菜单屏幕,以查看它是否只会重置而不会重置。我不确定到底发生了什么。有人遇到过这个问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?