Tips for importing CSS module into React with JSX (Troubleshooting)

When working on my react application, I encountered an issue where I needed to locally style one of my components. To achieve this, I decided to follow the yourCSS.module.css convention.

Within my myComponent.jsx file, I imported the CSS-Module as shown below:

import React, { Component } from 'react';
import styles from './startPage.module.css' //loading the css module

class StartPage extends Component {
    state = {  }
    render() { 
        return ( 
           <div className="container">
               <div className="row">    {/*Row 1 - contains title*/} 
                    <div style className="col-md-6 offset-md-3">
                        <p styles={styles.boilerplate}>some boilterplate text</p>
                    </div>
               </div>

               <div className="row">    {/*Row 2 - contains buttons*/} 

               </div>
           </div> 
         );
    }
}

export default StartPage;

However, upon testing the component, I encountered an error in the browser:

Error: The `style` prop expects a mapping from style properties to values, not a string.
For example, style={{marginRight: spacing + 'em'}} when using JSX.

Currently, my CSS includes the following styling for the `.boilerplate` class:

.boilerplate {
    background-color:#000000;
    border: black dotted;
} 

If anyone has any insights or suggestions on how to resolve this issue, it would be greatly appreciated!

Answer №1

It is recommended to utilize the styles attribute over the style attribute

<div style className="col-md-6 offset-md-3">

</div>

Alternatively, you can simply eliminate the style attribute as it currently has no value!

Answer №2

It seems like there is a mistake:

// delete the style prop as it will end up being converted to style={true}
<div className="col-md-6 offset-md-3">
// opt for "style" prop instead of "styles"
  <p style={styles.boilerplate}>some boilterplate text</p>
</div>

Answer №3

For guidance on utilizing inline css in react, please refer to the documentation here.

When incorporating styles into your components, consider using ClassName to point to classes defined in an external CSS file

import React, { Component } from 'react';
import './startPage.module.css' //including a css file here

class StartPage extends Component {
state = {  }
render() { 
    return ( 
       <div className="container">
           <div className="row">    {/*Row 1 - contains title*/} 
                <div className="col-md-6 offset-md-3"> // remove style
                    <p className='boilerplate'>some boilterplate text</p> // add boilerplate as ClassName
                </div>
           </div>

           <div className="row">    {/*Row 2 - contains buttons*/} 

           </div>
       </div> 
     );
}
}

export default StartPage;

If you opt to use the style attribute, it can be implemented like this:

import React, { Component } from 'react';


const divstyles = {
    backgroundColor: '#000000',
    border: 'red dotted'
}

class StartPage extends Component {
state = {  }
render() { 
    return ( 
       <div className="container">
           <div className="row">    {/*Row 1 - contains title*/} 
                <div className="col-md-6 offset-md-3">
                <p style={divstyles}>some boilterplate text</p>
                </div>
           </div>

           <div className="row">    {/*Row 2 - contains buttons*/} 

           </div>
       </div> 
     );
}
}

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

What is the process for adding a gradient color to a button placed on a textbox

I am looking to create a textbox and textarea that include gradient effects similar to the image above. I have managed to implement it for a button. However, I'm having difficulty applying it to a textarea element. ...

CKEditor's height automatically increases as the user types

I am using a ckEditor and looking for a way to make its height automatically grow as I type. https://i.stack.imgur.com/m7eyi.png <textarea name="description" id="description"> </textarea> <script> CKEDITOR.replace( 'description ...

Padding to enhance the appearance of anchor tags

Currently, I have an asp.net page with the following code displayed below: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh ...

Enhance Your Website with Dynamic Navigation Animation

Seeking assistance with animating a navigation bar on and off the screen from the left side. The nav is not animating as expected using the latest version of jquery.min.js. The first step was to animate the nav upon clicking an object. View the current pro ...

In Internet Explorer, the Textarea fails to display new lines

It appears that when text is placed into a textarea, IE ignores \r\n while FF/Chrome/Opera do not. Here's how the rendering differs: Paragraph1 sometext Paragraph2 othertext In IE7/8, however, it renders as: Paragraph1 sometextParagraph2 ...

jQuery "ooze navigation" - precise pixel rounding

After successfully programming my jQuery slime menu, I am facing a minor issue. Although the menu works beautifully, I have noticed that when I move the arrow and the ending circle of the slime separately, they do not always connect accurately if the curso ...

Express - An error occurred: Unable to access the property 'prototype' as it is undefined in the file request.js on line 31

My time has been consumed trying to troubleshoot this issue, yet I am puzzled by its origin and the reason behind this error. I am in the process of creating a basic website to hone my skills in React and have been attempting to retrieve data from Riot&ap ...

Obtain asynchronous result from updating state in React

I am trying to achieve the following: myFunction = () => { this.setState( state => { const originalBar = state.bar; return { foo: "bar" }; }, () => ({ originalBar, newBar: state.foo }) //return this object ...

Developing User Registration functionality in Keycloak for both Flutter and React User Interfaces

I am currently working on a project that involves a blend of Flutter frontend, React frontend, and a Spring Boot backend. My goal is to manage authentication using Keycloak, but I have hit a roadblock. It seems that I am unable to locate a REST endpoint in ...

Having trouble with overriding an @media query for material-ui react components?

I've been trying to modify a @media CSS rule on a Material UI component, similar to the discussions on How to over-ride an @media css for a material-ui react component and Override components like MuiTab that use media queries. However, I have not bee ...

Utilizing React with Emotion to Customize Font Sizes

In my React app, I am using emotion to style and display the formula for compound interest. Within my render() method, I am returning the following: <div css ={equation}> <p> P (1 +</p> <p css={fraction}> <span classNa ...

Exploring the application of styles within the shadow DOM

I'm experimenting with applying styles to the shadow DOM. Consider the following example: const element = document.getElementById("bar"); const shadowRoot = element.attachShadow({ mode: "open" }); const greeting = document.createElement("p"); gree ...

Move the monitoring role from the server to the client side

I am trying to transfer an observer from my server to the client in order to subscribe to it. @SubscribeMessage('createTodo') public handleCreateTodo( @MessageBody() data: IDataForCreateTodo, @ConnectedSocket() client: S ...

Is utilizing React's useEffect hook along with creating your own asynchronous function to fetch data the best approach

After attempting to craft a function for retrieving data from the server, I successfully made it work. However, I am uncertain if this is the correct approach. I utilized a function component to fetch data, incorporating useState, useEffect, and Async/Awa ...

Steps for changing an image with another image upon button click in Angular 6

I have a button with an image and text. When the button is clicked, I want to change both the image and content. Here's the code I'm currently using: <div class="btn-default"> <button name="Save" [ngClass]="[bntStyle]" (click)="submit ...

What is the best way to arrange these containers in a horizontal stack?

Check out the code snippet at: https://codepen.io/shayaansiddiqui/pen/YzGzeOj I'm looking to align these boxes horizontally, how can I achieve that? <div> <div class="container"> <img src="#" ...

Inline responsive pictures

I am attempting to align 2 images side by side while also ensuring they are responsive using Bootstrap. I have attempted to use the following code: <img src="..." class="float-left" alt="..."> <img src="..." class="float-right" alt="..."> wh ...

Is there a more effective approach to managing an array of objects retrieved from an API call?

I'm attempting to extract an array of names from a JSON response that contains an array of objects, but I'm running into issues. When I try to print the array, it shows up empty. Is this the correct way to go about it? There don't seem to be ...

"Aligning content in Bootstrap with vertical centering

I am currently working on aligning a simple piece of code vertically. Check out the code on JS Fiddle: https://jsfiddle.net/3kj44net/ .container-fluid { padding-left: 20px; padding-right: 20px; } <link href="https://maxcdn.bootstrapcdn.com/boots ...

IE6/7 dilemma with CSS floating

Encountering a strange CSS float issue in IE6 and IE7. The HTML code looks like this: <fieldset style="float:left"> <legend>Summary</legend> <div class="display-label">Recruitment type</div> <div class="displa ...