React css from module doesn't apply (no webpack) - css

I've a react app, which has several modules.
For one module, the css or scss doesn't apply.
all other modules does apply the scss. I've changed already from css to scss but doesn't work.
I've tried also to use !importent at the background, but in the developer console, the scss doesnt appear at all. Like scss is not found.
App.js
import React, {useEffect, useState} from 'react'
import './App.scss'
import EditView from "../editView/editView";
return (
//some other html stuff
<div className="app_editContent">
<EditView action={actionToPerform}/>
</div>
)
EditView.js
import React from 'react';
import './editView.module.scss';
import {faSave, faStopCircle, faSun} from "#fortawesome/free-solid-svg-icons"
import {FontAwesomeIcon} from "#fortawesome/react-fontawesome";
export default class EditView extends React.Component {
constructor(props) {
super(props);
}
showTitle() {
return (
this.props.action === "new" ? (
[
<div className="edit_container">
<div className="edit_newListTitle">Neue Shoppingliste erstellen</div>
<label className="edit_titleInputLabel">Titel</label>
<input type="text" className="edit_titleInput" name="title"></input>
<label className="edit_locationInputLabel">Ort</label>
<input type="text" className="edit_locationInput" id="location" value=""></input>
<label className="edit_priorityInputLabel">Wichtigkeit</label>
<input className="edit_priorityInput" type="range" id="priority"
min="1" max="5" step="1"></input>
<label className="edit_toDoDateInputLabel">Erledigen bis</label>
<input className="edit_toDoDateInput" type="datetime-local" id="date"></input>
<input className="edit_amountInput" type="number" id="quantity" name="quantity" min="1"
value="1"></input>
<input className="edit_itemInputLabel" type="text" id="item" value=""></input>
<button className="edit_buttonSave"><FontAwesomeIcon icon={faSave}/></button>
<button className="edit_buttonCancel"><FontAwesomeIcon icon={faStopCircle}/></button>
</div>
]
) : this.props.action === "edit" ? (
<div>edit</div>
) : (
<div>nothing to show</div>
)
)
}
actionToPerformChange = (val) => {
this.setState({actionToPerform: val});
this.forceUpdate()
}
render() {
return (<div>
<button onClick={() => this.actionToPerformChange('new')}>Create new Shoppinglist</button>
<button onClick={() => this.actionToPerformChange('edit')}>Edit Shoppinglist</button>
{this.showTitle()}
</div>
)
}
}
editView.module.scss
.edit_container {
height: 100%;
display: grid;
grid-template-columns: 120px 1fr;
grid-template-rows: auto;
background: yellow !important;
}
The other scss modules are build the same way but they do work.

I've found the answer shortly after posting. If using classNames need to define it in {} like :
className={styles.edit_container}

Related

react section displayed as div

I tried to apply a style to a section using the styled-components module in react.
However, the style was not applied to the section, so I checked it and it is displayed as a div on the console.
// GlobalStyles.tsx
section {
width: 100%;
height: 100%;
display: flex;
}
// Login.tsx
import styled from 'styled-components';
const Section = styled.section`
background-color: #ff69b4;
`;
const SERVER_ENDPOINT = 'http://localhost:3001';
function Login() {
...
return (
<Section>
<form>
<input
type="text"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<input type="submit" value="Login with Spotify" onClick={handleLogin} />
</form>
<span>{errorMsg}</span>
<Link to="/join">Sign Up</Link>
</Section>
);
}
export default Login;
console html
how should i solve this problem?

CSS styles are not being applied to React file

I am working on a web based project. All code is executed but the styles are not being applied to files. Below is code of my react file. I also added my css file below it.
I tried using .sass extension which is also not working.
Can anyone help me with it? How do I apply styles?
import "./App.css";
import React, {useState, useEffect} from "react";
import Axios from "axios";
function App(){
const [searchVal,setSearchVal]=useState('');
//getting values from DB
return <div>
<input type="text" placeholder="Search College" onChange={event =>{setSearchVal(event.target.value)}}></input>
{collegeList.filter((val)=>{
if(searchVal==""){
return val;
}else if (val.Name_of_College.toLowerCase().includes(searchVal.toLowerCase())){
return val;
}
}).map((val)=>{
return <div className="clgListDiv" > <p>
College Name: {val.Name_of_College} <br></br>
</p>
</div>
})}
</div>
}
export default App;
This is the css file
.App{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
font-family: Arial, Helvetica, sans-serif;
}
.APP input{
margin-top: 20px;
width:350px;
height: 40px;
font-size: 20px;
padding-left: 10px;
}
Well it doesn't work because you didn't add className attribute in JSX code in first div and maybe you should rename .APP input to .App input in your CSS file as you also want same styling in input tag also.
import "./App.css";
import React, {useState, useEffect} from "react";
import Axios from "axios";
function App(){
const [searchVal,setSearchVal]=useState('');
//getting values from DB
return <div className='App'>
<input type="text" placeholder="Search College" onChange={event =>{setSearchVal(event.target.value)}}></input>
{collegeList.filter((val)=>{
if(searchVal==""){
return val;
}else if (val.Name_of_College.toLowerCase().includes(searchVal.toLowerCase())){
return val;
}
}).map((val)=>{
return <div className="clgListDiv" > <p>
College Name: {val.Name_of_College} <br></br>
</p>
</div>
})}
</div>
}
export default App;
Best Practice in React:
import React, { useState, useEffect } from "react";
import "./App.css";
import Axios from "axios";
function App() {
const [searchVal, setSearchVal] = useState("");
//getting values from DB
const content = collegeList
.filter((val) => {
if (searchVal == "") {
return val;
} else if (
val.Name_of_College.toLowerCase().includes(searchVal.toLowerCase())
) {
return val;
}
})
.map((val) => {
return (
<div className="clgListDiv">
<p>
College Name: {val.Name_of_College} <br></br>
</p>
</div>
);
});
return (
<div>
<input
type="text"
placeholder="Search College"
onChange={(event) => {
setSearchVal(event.target.value);
}}
></input>
{content}
</div>
);
}
export default App;

Custom CSS is not getting applied in .Net Core React App

I am using Reactstrap and trying to add some custom CSS, but the custom CSS is not getting applied.
Here is the code snippet.
Below is Home.js file
import React, { Component } from 'react';
import { Row, Col, Button, Form, FormGroup, Label, Input } from 'reactstrap';
import './Home.css'
export class Home extends Component {
constructor(props) {
super(props);
this.state = { showForm: false, btnText: "Add Record", formClass: "formHide" };
this.addRecord = this.addRecord.bind(this);
}
addRecord() {
if (this.state.btnText === 'Back') {
this.setState({ btnText: 'Add Record' });
this.setState({ formClass: "formHide" });
} else {
this.setState({ btnText: 'Back' });
}
}
render() {
return (
<section>
<h1 className='centerTitle'>Welcome to Fit Buddy</h1>
<Row>
<Col sm={9}></Col>
<Col sm={3}>
<Button color="primary" onClick={this.addRecord}>{this.state.btnText}</Button>
</Col>
</Row>
<Form className={this.state.formClass}>
<FormGroup row>
<Label for="exampleSelect" sm={3}>Exercise Type</Label>
<Col sm={9}>
<Input type="select" name="select" id="exampleSelect">
<option>Push Ups</option>
<option>Sit Ups</option>
</Input>
</Col>
</FormGroup>
<FormGroup row>
<Label for="exampleReps" sm={3}>No. of Reps</Label>
<Col sm={9}>
<Input type="text" name="text" id="exampleReps" placeholder="Enter number of Reps" />
</Col>
</FormGroup>
</Form>
</section>
);
}
}
Below is Home.css file
centerTitle {
text-align: center !important;
}
formHide {
display: none !important;
}
I tried debugging using developer Tools, this is the code, I am able to see. My Style is there and it is applied correctly in HTML as well. Still, my style is not working.
Code in Browser
When trying to provide styling to HTML Tags, it is working. But when giving styles to class, it is not working.
Please help. Thanks in Advance.
Your css-file is not targeting anything, since you are missing the dots infront of your classnames.
.centerTitle {
text-align: center !important;
}
.formHide {
display: none !important;
}
Edit:
Here is a good overview of the different css-selectors:
https://www.w3schools.com/cssref/css_selectors.asp

How to set custom style to sementic ui components, reactjs

Here is my model for login page, the form fields have gained the full width of the modal. I want to set a margin of 10px around each input field.
import React from "react";
import ReactDOM from "react-dom";
import { Button, Input, Modal } from "semantic-ui-react";
import "semantic-ui-css/semantic.min.css";
function App() {
return (
<Modal dimmer="blurring" open={true} size={"medium"}>
<Modal.Header>Login</Modal.Header>
<Input fluid placeholder="Username" icon="user" />
<Input fluid placeholder="Password" icon="key" type="password" />
<Modal.Actions>
<Button
positive
icon="checkmark"
labelPosition="right"
content="Submit"
/>
</Modal.Actions>
</Modal>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
It is straight forward. But I couldn't find the documentation. I simply added styles directly to the component. it worked.
import React from "react";
import ReactDOM from "react-dom";
import { Button, Input, Modal } from "semantic-ui-react";
import "semantic-ui-css/semantic.min.css";
const inlineStyle = {
input : {
margin: '10px',
}
};
function App() {
return (
<Modal dimmer="blurring" open={true} size={"medium"}>
<Modal.Header>Login</Modal.Header>
<Input fluid placeholder="Username" icon="user" style={inlineStyle.input}/>
<Input fluid placeholder="Password" icon="key" type="password" style={inlineStyle.input}/>
<Modal.Actions>
<Button
positive
icon="checkmark"
labelPosition="right"
content="Submit"
/>
</Modal.Actions>
</Modal>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Why my form elements doesn't follow my grid layout and are displays as if they were blocks element

I'm working with grid css and my form elements doesn't wollow my grid convention.
I can't figure out why it fails since I have followed the grid requirements and ensured that form is itself a grid with two columns inside, one for the input, other for the submit button.
So it seems to me that they should be displayed as inline elements.
Demo here
here my react.js snippet :
import React, { Component } from "react";
import ReactDOM from "react-dom";
import style from "./styles.css";
class App extends Component {
state = {
name: "",
email: ""
};
onChange = e => {
console.log("e.target.name: ", e.target.name);
this.setState({ [e.target.name]: e.target.value });
};
sendForm = e => {
e.preventDefault();
console.log("form sent: ", this.state.name, this.state.email);
};
render() {
return (
<div className={style.newsletter_grid}>
<h2 className={style.newsletter_headline}> newsletter headline</h2>
<form className={style.newsletter_form} onSubmit={this.sendForm}>
<div className={style.newsletter_input}>
<input
onChange={this.onChange}
name="name"
type="text"
value={this.state.name}
placeholder="name"
/>
<input
onChange={this.onChange}
name="email"
type="text"
value={this.state.email}
placeholder="email"
/>
</div>
<button type="submit" className={style.newsletter_conversion}>
confirmer
</button>
</form>
</div>
);
}
}
const root = document.getElementById("root");
ReactDOM.render(<App />, root);
My react.css snippet:
.newsletter_grid {
display: grid;
width: 100%;
grid-template-areas:
"headline headline"
"form form";
}
.newsletter_headline {
grid-area: headline;
}
.form {
grid-area: form;
display: grid;
grid-template-areas: "newsletter_input newsletter_conversion";
}
.newsletter_input {
grid-area: newsletter_input;
}
.newsletter_conversion {
grid-area: newsletter_conversion;
}
If someone has any hint, would be great.
thanks.

Resources