为了解释我目前正在使用这个Reaction原生组件做什么,我隐藏了一些元素,如果我的屏幕中的Container_Menu_Header上有‘Options’.(添加或删除标题选项).

下面的一切都运行得很好,但我感兴趣的是用更少的代码编写相同的功能.当Container_Menu_Header具有Options属性时,必须有一种更简单的方法来删除或添加样式.隐藏样式.

import React from "react";
import { StyleSheet, ImageBackground, Text } from "react-native";

import colors from "../../config/colors";
import fonts from "../../config/fonts";

import Frame_Menu_Header from "../../components/Frame_Menu_Header";
import Frame_Icon from "../../components/Frame_Icon";

function Container_Menu_Header({ title, options }) {
  return (
    <Frame_Menu_Header>
      {options ? (
        <Frame_Icon top_left_menu={[styles.top_left_menu_frame_icon]}>
          <ImageBackground
            style={styles.icon}
            source={require("../../assets/_UI/Icon_Symbol_Checkmark.svg")}
          ></ImageBackground>
        </Frame_Icon>
      ) : (
        <Frame_Icon
          top_left_menu={[styles.top_left_menu_frame_icon, styles.hide]}
        >
          <ImageBackground
            style={styles.icon}
            source={require("../../assets/_UI/Icon_Symbol_Checkmark.svg")}
          ></ImageBackground>
        </Frame_Icon>
      )}
      <Text style={[styles.text, styles.h1, styles.menu_title]}>{title}</Text>
      {options ? (
        <Frame_Icon top_right_menu={[styles.top_right_menu_frame_icon]}>
          <ImageBackground
            style={[styles.icon, { height: 12 }]}
            source={require("../../assets/_UI/Icon_Symbol_Arrow.svg")}
          ></ImageBackground>
        </Frame_Icon>
      ) : (
        <Frame_Icon
          top_right_menu={[styles.top_right_menu_frame_icon, styles.hide]}
        >
          <ImageBackground
            style={[styles.icon, { height: 12 }]}
            source={require("../../assets/_UI/Icon_Symbol_Arrow.svg")}
          ></ImageBackground>
        </Frame_Icon>
      )}
    </Frame_Menu_Header>
  );
}

const styles = StyleSheet.create({
  Container_Menu_Header: {},
  menu_title: {
    marginTop: 10,
  },
  top_left_menu_frame_icon: {
    marginLeft: 8,
    marginTop: 9,
  },
  top_right_menu_frame_icon: {
    marginRight: 8,
    marginTop: 9,
  },
  text: {
    fontFamily: fonts.primary,
  },
  h1: {
    fontSize: fonts.h1,
    lineHeight: 16,
  },
  icon: {
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    width: 16,
    height: 16,
    marginBottom: 1,
  },
  hide: {
    opacity: 0,
  },
});

export default Container_Menu_Header;

推荐答案

如果您只想删除或添加styles.hide样式,可以这样勾选top_left_menu中的options:

function Container_Menu_Header({ title, options }) {
  return (
    <Frame_Menu_Header>
      <Frame_Icon
        top_left_menu={[
          styles.top_left_menu_frame_icon,
          options ? {} : styles.hide,
        ]}
      >
        <ImageBackground
          style={styles.icon}
          source={require("../../assets/_UI/Icon_Symbol_Checkmark.svg")}
        ></ImageBackground>
      </Frame_Icon>
      <Text style={[styles.text, styles.h1, styles.menu_title]}>{title}</Text>
      <Frame_Icon
        top_right_menu={[
          styles.top_right_menu_frame_icon,
          options ? {} : styles.hide,
        ]}
      >
        <ImageBackground
          style={[styles.icon, { height: 12 }]}
          source={require("../../assets/_UI/Icon_Symbol_Arrow.svg")}
        ></ImageBackground>
      </Frame_Icon>
    </Frame_Menu_Header>
  );
}
``

Javascript相关问答推荐

如何确保滚动时响应式图像保持在文本图像布局中的顶部?

详细说明如何以图表方式改变仪表的范围和 colored颜色

d3可排序表标题行中有收件箱--如何使收件箱不触发排序?

响应式JS DataTable中的Bootstrap 5弹出程序无法正常工作

获取最接近的父项(即自定义元素)

如何通过onClick为一组按钮分配功能;

如何使用Echart 5.5.0创建箱形图

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

从PWA中的内部存储读取文件

浮动Div的淡出模糊效果

Next.js(react)使用moment或不使用日期和时间格式

如何为我的astro页面中的相同组件自动创建不同的内容?

为什么!逗号和空格不会作为输出返回,如果它在参数上?

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

如何将未排序的元素追加到数组的末尾?

Nextjs 13.4 Next-Auth 4.2登录(&Quot;凭据&,{});不工作

为什么我的按钮没有从&q;1更改为&q;X&q;?

如何在一个对象Java脚本中获取不同键的重复值?

Phaser3 preFX addGlow不支持zoom

为列表中的项目设置动画