为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过单击 React Native 中的按钮来更改动态生成的 JSX 的样式

如何通过单击 React Native 中的按钮来更改动态生成的 JSX 的样式

胡说叔叔 2022-08-04 16:14:59
我正在根据从后端API获得的数组值生成JSX代码。如下图所示,我正在根据数组的长度生成这些框。我想要的是,当我点击这些框中的任何一个时,背景颜色都会发生变化。我希望这些框的行为类似于单选按钮,因此一次只有一个框具有不同的背景颜色。数组名称为“hasMultipleWeights”。我只包括了代码的相关部分...const ProductDetailsScreen = (props) => {  const productId = props.navigation.getParam("productId");  const selectedProduct = useSelector((state) =>    state.products.products.find((prod) => prod.id === productId)  );  const productsMultipleWeights = useSelector(    (state) => state.products.productsMultipleWeights  );  var hasMultipleWeights = productsMultipleWeights.find(    (prod) => Object.keys(prod)[0] == selectedProduct.id  );  if (hasMultipleWeights) {    hasMultipleWeights = hasMultipleWeights[Object.keys(hasMultipleWeights)[0]];  }return (    <ScrollView style={{}}>      <View style={styles.screen}>        {hasMultipleWeights && (          <View style={{ alignItems: "center" }}>            <ScrollView              horizontal              contentContainerStyle={{ padding: 2 }}              showsHorizontalScrollIndicator={false}            >              {hasMultipleWeights.map((item) => (                <TouchableOpacity                  key={item.id}                  onPress={() => {}}                  style={{                    ...styles.productOptions,                    backgroundColor: 'white',                  }}                >                  <Text style={styles.productWeightVolumUnit}>                    <Text style={styles.productWeightVolumeValue}>                      {NS(item.weight, "Arabic")}                    </Text>                    {"  "}                    {selectedProduct.weightVolumeUnit}                  </Text>                  <MaterialCommunityIcons                    name={                      selectedProduct.weightVolumeUnit === "كغ"                        ? "weight-kilogram"                        : selectedProduct.weightVolumeUnit === "مل"                        ? "water"                        : "weight-gram"                    }
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

要动态更改颜色,您必须使用状态。因此,创建一个新状态来检查“已选中”按钮,在 onPress 方法中更改它,然后进行验证。


const ProductDetailsScreen = (props) => {

  const [checkedButton, setCheckedButton] = React.useState('')


  const productId = props.navigation.getParam("productId");


  const selectedProduct = useSelector((state) =>

    state.products.products.find((prod) => prod.id === productId)

  );



  const productsMultipleWeights = useSelector(

    (state) => state.products.productsMultipleWeights

  );


  var hasMultipleWeights = productsMultipleWeights.find(

    (prod) => Object.keys(prod)[0] == selectedProduct.id

  );


  if (hasMultipleWeights) {

    hasMultipleWeights = hasMultipleWeights[Object.keys(hasMultipleWeights)[0]];


  }



return (

    <ScrollView style={{}}>

      <View style={styles.screen}>


        {hasMultipleWeights && (

          <View style={{ alignItems: "center" }}>

            <ScrollView

              horizontal

              contentContainerStyle={{ padding: 2 }}

              showsHorizontalScrollIndicator={false}

            >

              {hasMultipleWeights.map((item) => (

                <TouchableOpacity

                  key={item.id}

                  onPress={() => setCheckedButton(item.id)}

                  style={{

                    ...styles.productOptions,

                    backgroundColor: checkedButton === item.id ? "grey" : "white",

                  }}

                >

                  <Text style={styles.productWeightVolumUnit}>

                    <Text style={styles.productWeightVolumeValue}>

                      {NS(item.weight, "Arabic")}

                    </Text>

                    {"  "}

                    {selectedProduct.weightVolumeUnit}

                  </Text>

                  <MaterialCommunityIcons

                    name={

                      selectedProduct.weightVolumeUnit === "كغ"

                        ? "weight-kilogram"

                        : selectedProduct.weightVolumeUnit === "مل"

                        ? "water"

                        : "weight-gram"

                    }

                    size={26}

                    color="grey"

                    style={styles.weightIcon}

                  />

                </TouchableOpacity>

              ))}

            </ScrollView>

          </View>

        )}

      </View>

    </ScrollView>

  );

};


const styles = StyleSheet.create({


productOptions: {

    shadowColor: "black",

    shadowOpacity: 0.26,

    elevation: 5,

    borderRadius: 10,

    backgroundColor: "white",

    width: 85,

    height: 65,

    marginHorizontal: 6,

    alignItems: "center",

    justifyContent: "center",

    shadowRadius: 2,

    shadowOffset: { width: 0, height: 1 },

  },


});


查看完整回答
反对 回复 2022-08-04
?
偶然的你

TA贡献1841条经验 获得超3个赞

似乎需要创建状态checkedId


 const [ checkedId, setCheckedId ] = useState('')


 useEffect(() =>

    // set first box to red at first render

    hasMultipleWeights && setCheckedId(hasMultipleWeights[0].id) ,[ hasMultipleWeights ])



 ...


 <TouchableOpacity

              key={item.id}

              onPress={() =>setCheckedId(item.id)}

              style={{

                ...styles.productOptions,

                backgroundColor: item.id == checkedId ? 'red' : 'white',

              }}

            >


查看完整回答
反对 回复 2022-08-04
  • 2 回答
  • 0 关注
  • 215 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号