首页 > 解决方案 > 如何通过单击另一个组件的按钮使视图在一个组件中可见?

问题描述

我有两个组件 组件 A 有一个默认隐藏按钮,另一个组件 B 有搜索按钮 当我单击组件 B 中的按钮时,如何使组件 A 中的按钮可见,我在 A 中导入组件 B 如何实现上述功能?

标签: react-native

解决方案


当您激活一个按钮时,有很多方法可以浮动其他组件。

您可以更改状态值使其浮动,或者您可以通过键值传递值以接收数据并执行按钮,如下例所示。

例子.js

      <TouchableOpacity
              onPress={() => this.getItems()}
              activeOpacity={0.5}
              style={styles.btn}
              textStyle={styles.txt}
            >Get Products ({productList.length})</TouchableOpacity>
            {
              productList.map((product, i) => {
                return (
                  <View key={i} style={{
                    flexDirection: 'column',
                  }}>
                    <Text style={{
                      marginTop: 20,
                      fontSize: 12,
                      color: 'black',
                      minHeight: 100,
                      alignSelf: 'center',
                      paddingHorizontal: 20,
                    }} >{JSON.stringify(product)}</Text>
                    <TouchableOpacity
                      onPress={() => this.buyItem(product.productId)}
                      activeOpacity={0.5}
                      style={styles.btn}
                      textStyle={styles.txt}
                    >Buy Above Product</TouchableOpacity>
                  </View>
                );
              })
            }

推荐阅读