How do I pass color values as props in React.Js using Typescript?

As a newcomer to React, I have encountered a component where colors were previously hard-coded. My goal is to pass a color through props instead.

In addition, I am utilizing styledComponents for the styling, in case that affects anything.

      &[checkedcolor] {
    /*&[checkedcolor="orange"] {*/
      &:checked {
        + .lbl {
          border-color: ${props => props.theme.borderColor};
          color: ${props => props.theme.color};
        }
      }
    }
  }
  &[uncheckedcolor] {
    /*&[uncheckedcolor="lightgray"] {*/
      + .lbl {
        color: ${props => props.theme.color};
      }
    }
  }
}

} `;

This section of the styling involves passing a prop. I'm uncertain if I'm approaching this correctly.. Additionally, the "theme" is extracted from a themes.ts file which should be provided by a theme provider from another file. The commented out part indicates where the style was originally hard-coded.

  render() {
var { className, type, radioClasses, label, ...other } = this.props;
return (
  <RadioWrapper className={"radio-switch-item-wrapper " + radioClasses}>
    <input type="radio" className="ace radio-switch-item" {...other} />
    <span className="lbl">{" " + this.props.label}</span>
  </RadioWrapper>

);

} }

Here is the render function.. I'm somewhat unsure about what's happening here. Radiowrapper is the name of the styling being used. Moreover, I'm working on existing code belonging to someone else, hence my lack of complete understanding.

Answer №1

This is an example of how your style file could be structured:

const StyledContainer = styled.div`
  ... additional styles here
  background-color: ${props => props.bgColor}
`

And this is how your component file might look:

render() {
  return(
   <StyledContainer bgColor="#FF5733"/> 
  );
}

Answer №2

To apply a specific color to the component, you can pass it as a prop like this:

<CustomComponent textColor="#FF0000"/>

Next, you can access and utilize this prop within styled components by defining it in the following manner:

const CustomComponent = styled.div`
color: ${(props:{textColor:string}) => props.textColor}`;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for enabling an element to exceed the bounds of a scrollable container

I am facing an issue with my menu where it has grown in size, making it necessary to have a scroll function for easier navigation. I have successfully implemented the scrolling feature. However, within this menu is a submenu that should extend out to the ...

React/Redux requires a lineup of card components

I am in the process of creating a row consisting of 10 cards. Each card is required to make an individual API call to retrieve a rendition URL. How can I achieve this using React/Redux? Displayed below is the code snippet for my React Card component. I am ...

Triggering Leaflet's Mouseout function during a MouseOver event

I am working on a leaflet map project where I am dynamically adding markers. One of my goals is to have the marker's popup appear not only when clicked, but also when the user hovers over it. This is the code snippet I have been using: function mak ...

Utilizing jQuery to Calculate Tab Scores

Last week, my teacher and I collaborated on a fun game called rEAndom game (). The game is created using javascript, jQuery, and HTML5. One interesting feature of the game is that when you press the TAB key, a div displaying the score appears. You can chec ...

Experiencing discrepancies in pixel height while conducting tests using Nightwatch

Let me start by sharing some details about my machine setup: Operating System: CentOS 7 Graphics Drivers: kmod-nvidia (dedicated GPU) I am currently testing a webpage using Nightwatch, and one of the requirements is to ensure that a background image&apo ...

Make Bootstrap Panel Full Width with Highcharts Data

I am currently working on displaying graphs using the Highcharts library on three televisions. All televisions have a FULL HD resolution of 1920 x 1080. I have one Bootstrap panel containing a graph in the panel-body. <div class="panel panel-blue"> ...

The <Link> component in Next.js redirects to a 404 error page

I recently developed a next.js application and encountered an issue when attempting to link to another page. Whenever I try linking, I receive a 404 error. I have read that this is a known bug, but I am unsure of how to resolve it. Upon creating the app, I ...

Interactive Bootstrap 4 button embedded within a sleek card component, complete with a dynamic click event

I am trying to create a basic card using bootstrap 4 with the following HTML code. My goal is to change the style of the card when it is clicked, but not when the buttons inside the card are clicked. Currently, clicking on the test1 button triggers both ...

Appending an empty <li> tag has no effect

I'm facing an issue with this loop where it always generates empty <li></li> tags. Can anyone help me understand why this is happening and suggest a solution? For reference, the loop runs 2 times (verified). function a(){ for (var i ...

Tips for preserving changes made to a jQuery script when the page is reloaded

Is there a way to retain jQuery script changes when the page is reloaded? I have a page where clicking on certain elements triggers events, and I want these changes to persist even after reloading the page, resetting only when the cache is cleared. I appre ...

Best Practices for Implementing an Autocomplete Search Box in HTML

I'm currently working on implementing the code below and I could really use some guidance. Despite reading and watching Google Developers' YouTube videos, I am still struggling as I am new to this. My main goal is to integrate an autocomplete sea ...

Axios is unable to retrieve data in React render, but it is successful when accessed through the console

I am currently in the process of developing an e-commerce website and encountering an issue with rendering image data stored in MongoDB. The goal is to fetch the image data using axios and display it on the website. However, despite successfully fetching t ...

I'm stuck trying to figure out all the parameters for the MapsPage component in Angular 2

Currently, I am utilizing Angular2 with Ionic2 for my mobile app development. Everything was working flawlessly until I decided to incorporate a new module for Google Maps navigation. Specifically, I am using phonegap-launch-navigator for this purpose. The ...

What is the method for animating the hiding of <details>?

Having successfully used @keyframes to animate the opening of the details tag, I am now facing the challenge of animating the closing. Unfortunately, I have not been able to find a CSS solution for this. The @keyframes for closing are not working as expect ...

Guide to transforming a random hash into a visually appealing HTML table

After exploring HTML::QuickTable, I've noticed that it seems to only support a one-level-deep hash. I couldn't find a way within this module to specify the colspan and rowspan for certain headers when dealing with a multi-level hash structure. Is ...

What is the best way to update specific content with fresh URLs?

I am interested in keeping certain sections of my page static, such as the header and footer, while allowing the main content to change dynamically. Additionally, I would like the URL to update based on the new content being displayed. Although I am famil ...

Exploring the possibilities with JavaScript options

I am currently working on a project that involves a dropdown list... to complete unfinished sentences. Unfortunately, I am facing an issue with a message stating Uncaught TypeError: Cannot read property 'options' of null Below is a portion of m ...

What is the appropriate React children type for components that can only accept text or falsy values?

Can you help determine the type of children that will be allowed? <MyTextOnlyComponent> child {" foo "} another child {false && "should not be shown" } </MyTextOnlyComponent> To condense the children into a ...

Best practices for styling column headers in HTML tables

Developing a web application using Angular Material has been my ongoing project. I have created an md-list with 7 columns to display different values. My goal is to add a header to each column to identify its corresponding value. While I have the headers r ...

Tips for displaying Modal Bootstrap at the bottom of the screen

Is there a way to configure Bootstrap so that the modal appears at the bottom of the browser? https://jsfiddle.net/9fg7jsu3/ <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Modal test</button& ...