React Native - Native Base FAB(浮动操作按钮):点击 FAB 会自动调用子按钮“onpress”

IT技术 reactjs react-native uibutton floating-action-button native-base
2021-05-10 17:49:02

我目前正在尝试从原生框架中实现浮动操作按钮 (FAB)。我只是简单地复制了代码,并onPress在其中一个子按钮(在本例中为 Facebook-Logo)添加了一个方法。但是,这onPress是在我单击 FAB 按钮时触发的,而不是在单击 FAB 按钮后应出现的 facebook 按钮。

简而言之:所需的行为:1.点击FAB按钮2.由于数字1,让facebook按钮出现3.点击facebook按钮触发 onPress

我得到的行为: 1. 单击 FAB 按钮 2.onPress触发 facebook 按钮中的定义

我并没有真正更改代码中的任何内容,如下所示:

<Fab
                active={this.state.fabActive}
                direction="up"
                containerStyle={{ marginLeft: 10 }}
                style={{ backgroundColor: '#5067FF' }}
                position="bottomRight"
                onPress={() => this.setState({ feedbackComponentVisible: !this.state.fabActive })}>
                <Icon name="share" />
                <Button style={{ backgroundColor: '#34A34F' }}>
                    <Icon name="logo-whatsapp" />
                </Button>
                <Button style={{ backgroundColor: '#3B5998' }} onPress={() => this.setState({feedbackComponentVisible: true})}>
                    <Icon name="logo-facebook" />
                </Button>
                <Button disabled style={{ backgroundColor: '#DD5144' }}>
                    <Icon name="mail" />
                </Button>
</Fab>
1个回答

问题是这个代码段改变了两个 onPress 函数的相同状态。根据此 if this.state.fabActive=falsethen 值将更改为true当您单击子按钮期望的 FAB 按钮时。尝试使用两个单独的状态,这应该可以工作。