How can i change the classes generated by styled components in react? - reactjs

The classes generated by styled components are : sc-igwadP, Is there any way I can customize it to be something like that? => Icon_sc-igwadP

It is possible with a Babel plugin: babel-plugin-styled-components.
One of the default options from this plugin makes CSS classes easier to identify:
Better debugging: This option enhances the attached CSS class name on each component with richer output to help identify your components in the DOM without React DevTools. In your page source you'll see: <button class="Button-asdf123 asdf123" /> instead of just <button class="asdf123" />.
More info here

Related

Other component styles are getting applied to the component which I have not imported

Hello I am working on simple crud application in react js 18.0.0. My problem is I have my own styles for one component say eg..Home. But the styles which I have used for other components is also getting applied to Home component even though I did not imported it. Can anyone explain why?
I have attached an image for your reference.
In the above image I was in home component. But if you see the styles the container class in forgetPassword.css and login.css is also getting applied in home component. but In home component I did not imported those two css files(forgetPassword.css and login.css)
Yes this is because by default react does not support css or styles.
you can either use css modules(https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/)
Or
Use styled components.
I suggest you to try the css modules as that will be beginner friendly and easy.
You need to move your css to seperate file and name it [filename].module.css
In your case Home.module.css
Then in your Home.js component import it like import styles from './Home.module.css'
Then in your component use it like
<div className={styles.container} > ... </div>
I also recommend you not to modify the original bootstrap classes, instead create your custom class and add the overrides there.
eg:
<div className={`${styles.container} ${styles.home-container}`} > ... </div>

Custom svg image "button" for React Router Link

I am trying to customize a React Router Link "button" with a custom SVG image. I have found different sources with custom icons to be used, or solution like this but did not succeed with custom image file.The Router link is part of a dynamic list:
<td><Link to={} data-id={} className={"btn btn-success"}>Add new image</Link></td>
And the text would be moved as a hint upon mouseover effect. Any suggestion would be highly appreciated.
Update 1:
So i have tried with and with src. Of course it didnt render and run into error.
I have imported
import { ReactComponent as AddNewImageIcon } from "./WebAppButtons/AddPhoto.svg";
<td><Link to={``} data-id={} className={""}>{<AddNewImageIcon />}Add new image</Link></td>
In this way, it renders, however, there is a weird image background appears under the image:
Screenshot
Did not experience it with SVG files before. Is there any way to remove it or is it the behavior of SVG file in react? The other question is, should it be wrapped into DIV that would be formatted with SCSS or this component can be formatted with SCSS as well, eg.:width/height/hover etc.?

React JS and css pseudo element ::after ::before

As a React beginner I'm currently struggling with it for rendering styled components with the use of pseudo elements ::after and ::before.
Here is the sass style for a button: https://jsfiddle.net/r8qwhfvx/
It is what I try to achieve in React: changing the style of my button on hover using pseudo elements.
But how to use ::after / ::before in the className of button components for instance? JSX syntax unable the use of ::after / ::before in the components className.
I'm just able to write this:
return(
<button className='infoBtn'></button>
);
Please check jsfiddle link.
className JSX attribute is a string variable, which will be set as the class attribute value on the HTML element when it will be rendered by React. CSS pseudo elements can appear in CSS code, not as a class name for a HTML element.
So you need to give a class name to your button using className JSX attribute, then define CSS rules for this class. To do so you need to choose a strategy to setup CSS in your React app. There is multiple solutions :
use a plain old CSS file imported in your index.js or index.html
use a plain old style element in your index.html (these 2 options are global, like in plain old 90's web development)
use JSX "style" attribute : <Component style={{here put CSS object}} /> : https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
use CSS modules (if you are using create-react-app) : https://create-react-app.dev/docs/adding-a-css-modules-stylesheet/
use styled-components or any other CSS-in-JS option, there is a lot of solutions, I mention styled-components since it is one of the most popular
With all of these options you will be able to use CSS pseudo-element. For SASS you may have to setup a CSS preprocessor. If you are using create-react-app (https://create-react-app.dev/docs/adding-a-sass-stylesheet/) or styled-components (https://styled-components.com/docs/faqs#can-i-nest-rules) you're good to go, it's build in. Well for styled-components I'm not sure if it is a true full SASS support though.
Good luck with that !

Styling components and react-bootstrap

I'm fairly new to React and started checking out React-Bootstrap and the way the components are styled. The documentation clearly styles components with variant:
<Button variant="primary">Success</Button>
However, this code seems to work just as well with the traditional className. Functionally, and aesthetically, if I change the above code to something like,
<Button className="bg-primary">Success</Button>
the output is the exact same. Personally, I find using the className attribute much easier as I can write vanilla bootstrap classes like btn btn-primary... instead of different react-bootstrap classes.
Is there any functional difference between the two and is there a reason to use variant over className?
The difference between variant and className:
Variant:
- uses only pre-defined classes
- no need for an external library
className:
- can use custom classes
- works the same as the HTML attribute class
I personally use className
I personally don't use React-Bootstrap, preferring to include the CDN to Bootstrap and apply classes to my own components using className.
As far as I can tell, there is no functional difference between both methods.
Variant is a prop for your React-Bootstrap components which wrap the various predefined styles for the different bootstrap components. You can't customise a React Bootstrap component using a variant(if you want to deviate from bootstrap's theme).
ClassNames are your normal CSS classes wherein you can add any style to components through CSS. The reason className="bg-primary" would work because React-Bootstrap has the bootstrap's css file included as part of the library.
this is the bootstrap Button Component:
const Button = props =>{
const variant = props.variant?`btn-${props.variant}`:""
return(
<button className={`btn ${variant} ${props.className}`}
onClick ={props.onClick}>
</button>
)
}

React GeoSuggest with Material UI

I'm using this React auto-suggest address component like this:
<Geosuggest
name="adresse"
onSuggestSelect={this.onSuggestSelect}
onBlur={this.onSuggestSelect}
autoComplete="on"
/>
It works nicely but I'm wondering how can I render a Material UI input instead of the one rendered by the library.
Otherwise, is there any way for me to retrieve the className applied to an input? For instance: MuiInput-input-72 MuiInput-inputSingleline-79
Also, I'm using next.js so SSR is important so I can't easily play with style but className only.

Resources