Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format.

let _columns = [];

let commonStyle = {"width":"200","height":"300"};

_columns.push({
   style:commonStyle
});

When adding new properties, you can directly write them out:

_columns.push({
   width: 200
});

This method works, or you can use a variable (holding a single property value) like this:

let commonWidth = 200;

_columns.push({
   width: commonWidth 
});

However, I am facing issues when trying to utilize an array like 'commonStyle' with multiple properties/values. Is my syntax incorrect? Even when reducing the array to a single property, such as:

let commonStyle = {"width":"200"};

the style does not seem to apply... What could be causing this issue?

Answer №1

The examples you provided result in a distinct object layout compared to the initial example, in which the properties are nested within the "style" property identifier. Moreover, commonStyle should not be an array but rather an object with defined attributes.

Consider this alternative approach:

const commonStyle = {width: 200, height: 300};
_columns.push(commonStyle);

Answer №2

When dealing with shared styles in multiple components within a sizable project, it's advisable to opt for creating a separate .css or .less file to contain them.

.section{
    width: 250
    height: 350
}

Subsequently, apply the className='section' property instead.

By embedding styles directly into the codebase, future refactoring and restyling tasks could become more challenging and lead to poor design practices.

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

Inability to update Vue.js component data even after a successful GET request

I am encountering an issue with a component that initially contains a default object, and then fetches a populated object via a GET request upon creation. Despite successfully updating this.profile within the method, the changes do not seem to propagate ...

Using Props.store does not function properly when passed down to child components

Can anyone offer assistance with this issue? The problem I'm encountering is that 'this.props.store' does not seem to be accessible in child components. However, the connect(mapStateToProps, mapDispatchToProps) function works perfectly fin ...

The value of req.headers('Authorization') has not been defined

I'm experiencing difficulty with my code as the token is coming back as undefined. Here is the frontend section: export const fetchUser = async (token: any) => { const res = await axios.post('/user/getuser', { headers ...

Increase the amount of money in my current balance using React

I am working on an app that involves handling axios responses for different HTTP methods like post, get, etc. I have a component that resembles a credit card interface, where users can input an amount of money and then click on the "add money" button to ...

Passing a service into a directive results in an undefined value

Can someone help me understand why a service I am injecting into a directive is returning undefined in certain instances? If you would like to take a look at the code, here is a plunker link: https://plnkr.co/edit/H2x2z8ZW083NndFhiBvF?p=preview var app = ...

One may wonder about the distinction between running `npm i` and `npm i <package name>`

I am currently working on a React Native project where the package.json contains a specific version of react. However, I am hesitant to run npm i for fear that it will install the latest version of react instead. I wonder if running npm i will respect th ...

What are the differences between ajax loaded content and traditional content loading?

Can you explain the distinction between the DOM loaded by AJAX and the existing DOM of a webpage? Is it possible to load all parts of an HTML page via AJAX without any discrepancies compared to the existing content, including meta tags, scripts, styles, e ...

Tips for accessing private variables

After running this code in nodejs, I noticed that the 'Count' becomes negative for large values of 'count'. It made me wonder, is it the fault of 'count' or 'chain'? What would be the best approach to modify the &apo ...

Utilizing webpack HMR (Hot Module Replacement) independently of the webpack-dev-server

After successfully implementing HMR with webpack and seeing updates in the console when making changes, I have encountered an issue. While it works smoothly on the client side using webpack-dev-server, the server side setup with webpack/hot/poll requires m ...

Should the package for icons in the library I'm constructing be categorized under dependencies or devDependencies?

As I embark on creating my inaugural React component library, I find myself incorporating the flowbite and react-icons libraries. Despite familiarizing myself with their distinctions, I'm uncertain about where exactly they should be placed. Are these ...

Tips for sorting items in Wordpress with JavaScript / on click?

I am currently utilizing a method similar to the one outlined in this resource from w3 schools for filtering elements within divs. However, I am facing challenges when attempting to integrate this script into my Aurora Wordpress site due to the removal of ...

Can you explain the significance of "q" and "+" within the CSS properties of the HTML body tag?

This solution for question #2 on CSSBattle is considered one of the best. However, the use of the ""+"" sign and ""q"" in this single line of code is perplexing. <body bgcolor=62375 style=margin:0+50;border:dashed+53q#fdc57b;clip-pat ...

Mastering the Art of Merging 3 Arrays

Greetings to the Community I have a question regarding my code. I am looking to merge three variables together. $result = mysqli_query($con, "SELECT disease,age,SUM(CASE WHEN gender = 'm' THEN 1 ELSE 0 END) AS `totalM`, SUM(CASE WHEN gender ...

Having trouble retrieving the global variable within a while loop

I'm facing a challenge while working on this coding problem. It seems that I can't access the global variable within the while loop, as it returns undefined whenever I try to do so. function calculateSum(arr1, arr2, arr3) { let sum1 = 0; l ...

A dynamic jQuery plugin that replicates the smooth page sliding animation found in iPhone apps

Currently, I am in search of a jQuery plugin that has the capability to navigate users to different pages on a website with a sleek sliding animation, similar to what we see in some widely used iPhone apps. Despite my best efforts to create this functional ...

Struggling to get Axios working in Node despite having it properly installed

I am encountering an issue with my Jasmine test that involves HTTP requests. Despite having Axios installed using the command npm install axios --save, I keep getting the error message axios is not defined. var request = require('axios'); var co ...

How can we access a value within a deeply nested JSON object in Node.js when the key values in between are not

In the nested configuration object provided below, I am seeking to retrieve the value associated with key1, which in this case is "value1". It's important to note that while key1 remains static, the values for randomGeneratedNumber and randomGenerated ...

Developing a feature in React Native to retrieve the user's location without properly updating and returning the received data

In the function below, I am attempting to retrieve the user's current location and update specific location properties: export const getCurrentLocation = () => { const location = { userLat: '5', userLng: '' } navi ...

Traverse Through multiple strings in double pointer of characters

Looking at a function with the signature below: void foo (char **bar); The 'bar' variable represents an array of strings without a specified length. I am faced with the challenge of writing a loop to check if each string in 'bar' mee ...

Displaying text and concealing image when hovering over a column in an angular2 table

I am currently using a table to showcase a series of items for my data. Each data entry includes an action column with images. I would like to implement a feature where hovering over an image hides the image and reveals text, and vice versa (showing the im ...