By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Easier way to enable styling based on component attributes being present or not
JavaScript

Javascript – Easier way to enable styling based on component attributes being present or not

Admin
Last updated: 2024/01/12 at 2:31 PM
Admin
Share
2 Min Read
Easier way to enable styling based on component attributes being present or not

Problem:

To explain what I’m currently doing with this React Native component, I am hiding certain elements if ‘options’ is present on the Container_Menu_Header within my Screen. (adding or removing header options).

Contents
Problem:Solution:

Everything below is working fine, but I am interested in getting the same functionality written with less code. There must be an easier way to remove or add the styles.hide styling when the Container_Menu_Header has an options attribute present.

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;

Solution:

If you just want to remove or add styles.hide styling, you can check options inside top_left_menu like this:

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>
  );
}
``

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article JavaScript: Passing no parameter if option doesn't exist? Javascript – JavaScript: Passing no parameter if option doesn’t exist?
Next Article How to perform calculations with useState variables that handle increment and decrement Javascript – How to perform calculations with useState variables that handle increment and decrement
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?