Dynamic styles object for React components with inline styles

I have a styles object

let styles = {
  step_div:{
    height:'150px',
  } 
}

I'm trying to display multiple div elements with different colors using React

class Layout extends React.Component{
  constructor(props) {
    super(props);
    this.state={
      steps_array:[
        {title:'Intro',
          types:['Gate Keeper', 'Target Prospect'],
          color:'blue'},
        {title:'Grab Attention',
          types:['Name Drop', 'Product Value'],
          color:'yellow'},
        {title:'Disqualify Statement',
          types:[],
          color:'yellow'}]

When I try to set the border color dynamically based on each item's color property from the array, I encounter an error

  make_structure(){
    let rows = []
    let data = this.state.steps_array
    data.forEach((i, key)=>{
      let style = Object.assign({}, styles.step_div)
      style.border="1px solid "+i.color //each items color property
      let row = (
        <Row>
          <div  style={style}>
            {i.title}
          </div>
        </Row>
      )
      rows.push(row)
    })
    return rows
  }

The error message reads:

TypeError: Cannot assign to read only property 'border' of object '#<Object>'

Answer №1

One possible solution is shown below.

const updatedStyle = {...currentStyle.secondary,border:'1px dashed'+color.hex};

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

Challenge of integrating React Router with Express GET requests

I am struggling to understand how react router and express routes work together. This is what I currently have set up: app.get('*', function(req, res) { res.sendFile(path.resolve(__dirname) + '/server/static/index.html'); }); // ...

What is the best way to assign a value to an option element for ordering purposes?

My select element is being populated with fruits from a database, using the following code: <select name="fruitsOption" id="fruitsOptionId" ngModel #fruitRef="ngModel"> <option *ngFor="let fruit of fruits">{{fruit}}</option> </selec ...

Ensured static element-UI table dimensions even with modifications to columns

Creating an Element-UI table and using v-if to control column show/hide has been working perfectly, except for one small issue. The table seems to automatically change size when columns are shown/hidden, even though I have already set fixed width and heig ...

Ways to specify the encoding for a text iframe embedded in a webpage

Can anyone help me with document embedding in HTML? In my current project, I am directly embedding my update logs as text files into the webpage, along with a selection menu to view all the updates. However, I ran into a minor issue where the Firefox cons ...

Showing a collection of objects in a React component

**Recently started learning React and Node, and decided to fetch data into a functional component by following various tutorials. I successfully set up the server, connected it to the database, and fetched the data in React as per the tutorial instruction ...

What is the process for requesting a specific condition using jscript and jquery?

I'm experimenting with the following code: while (true) { $(document).ready(function() { setInterval(function () { if ($("h2").text() == "What is a light-year?") { $("#choice2").delay(200).queue ...

Scroll within the inner container

I am currently working on creating panels, each with a header and content. My goal is to allow scrolling only within the content area. Here is the CSS I have been using: .content-div{ float:left; height: 400px; overflow-x:hidden; overflow-y:hidden; ...

What is the method to retrieve a string from a specific element in Selenium using Python?

I'm facing a challenge in extracting a specific string from the following HTML element: <textarea class="FormTextArea__textarea form-control" aria-label="Photo Caption" aria-describedby="photo-caption__help" id="photo-caption" rows="3">Exterior ...

Having trouble with AngularJS? Ng-switch not updating after ng-click?

Initially in my code, I check if a user has the ability to flag a discussion. If they do, I use ng-switch where upon flagging, a success message is displayed: <div ng-if="canFlag(discussion)"> <div ng-switch="isFlagging" ng-click="fla ...

Unable to view newly added components

I am currently in the process of setting up a website, focusing on arranging and formatting everything on the page before moving on to styling and adding JavaScript. I recently added three div tags after my form to create three separate columns in the ne ...

Creating animated content with Blender and Three.js

Recently, I acquired a 3D model of an umbrella with a pre-existing animation that showcases its opening and closing. After importing it into my project using Three.js, I played the animation. To my surprise, what I thought was one animation turned out to b ...

Challenge with CSS Box Shadow

Hey there, I've been attempting to add a shadow effect to a box inside another box but I'm struggling to achieve the desired outcome. <div class="box effect2"> <div class="box effect2"> Box inside a box <div> &l ...

Looking to retrieve the specific clientX and clientY JavaScript Position within a child div element on a Wordpress Page?

I've implemented a highlight hover map in JavaScript that displays an 'info box' div when hovering over a specific area at . However, I'm encountering an issue where the 'info box' doesn't appear precisely at the mouse lo ...

Most effective strategy for handling PHP processing delays

As a newcomer to web programming, I appreciate your patience with me. I have developed a web page that submits form data to a PHP exec() function. This function retrieves data from a website, processes it in different ways, and saves it on my server. Whil ...

The placeholder within my input moves up and down when switching the input type from password to text

Currently, I am encountering an issue with the styling of a standard input element in React. Specifically, the placeholder text moves up and down by about 2px when viewed on Chrome, while there are no problems on Safari. How can I go about resolving this i ...

Issue with formik onchange event not filling data in Material UI TEXTFIELD component

Greetings! I am currently working on a React project where I am managing the authentication process. I am using Material UI and Formik for validation and handling input changes. However, I encountered an issue with my onchange Formik handler in the TEXTF ...

Concentrate on Selecting Multiple Cells in ag-Grid (Similar to Excel's Click and Drag Feature)

Is there a way to click and drag the mouse to adjust the size of the focus box around specific cells in ag-Grid? This feature is very handy in Excel and I was wondering if it is available in ag-Grid or if there is a workaround. Any insights would be apprec ...

"Obtaining mouse coordinates in VueJS: A step-by-step guide

I am using a component that is activated by v-on:click="someMethod". Is there a way to retrieve the X and Y coordinates of the mouse click within this component? Extra context: The component I'm working with is an HTML5 Canvas element. ...

Customizing active-class in Vuetify

How do I customize the appearance of my v-list-item-group when it is in the active state? <v-list> <v-list-item-group v-model="day" color="green"> <v-list-item v-for="(item, i) in items" :key="i"> <v-list-item-content& ...

Refresh a div with Ajax once all data has been fully loaded

I am currently using an ajax refresh div function that reloads a specific div every 10 seconds. Occasionally, the div loads before receiving data from mysql. Is there a way to modify it so that it waits 2 seconds after reloading the div? <script type ...