React offers various methods to style components. In some cases, you might want to add additional styles to an external component or assign minimal styles to a JSX element within a component, for example. It becomes important to use a component-based styling approach instead of the conventional class-based CSS styling.
In this guide, you'll learn how to set the default styles to your React components.
Consider a simple React component that renders h1
with some text in blue color.
1<h1 style={{color:'blue'}}>Inline styling in React</h1>
JSX elements in React are essential components in themselves and by default have access to a style prop. This is used for inline styling as shown above. The two pairs of curly braces are a part of the syntax that indicates that the style prop must be assigned an object containing key-value pairs. Thus the style prop can be passed to any component from its parent component. Following this approach, the same styles can be set as the default style by passing an object containing the CSS properties and their values as key-value pairs inside the object.
Declare the same style used above in h1
at the top of the component inside an object called headingStyle
.
1const headingStyle={
2 color:'blue'
3}
Note: CSS properties separated by hyphen ( - ) are camel cased in JSX.
Pass this object inside the style prop.
1<h1 style={headingStyle}>CSS in JSX React</h1>
This is the general approach for setting default styles in React. The double pair of curly braces is reduced to a single one as an object itself is being assigned to the style prop. This concept of styling React components with an inline styling approach is also referred to as CSS in JS as you are directly using CSS properties in your JSX template.
Create a simple React component that renders an empty div
.
1const MyDivComponent=()=>{
2 return(
3 <>
4 <div/>
5 </>
6 )
7}
Now create a style object divStyles
containing some CSS rules to style the div
.
1const divStyles={
2 backgroundColor: 'teal',
3 padding: 10,
4 marginLeft: 100,
5 height: 400,
6 width:400
7}
divStyles
contains simple CSS rules to give a teal background color to the div
along with 400x400px dimensions, a 100px margin from the left, and 10px of padding.
Pass it down as props to the MyDivComponent
and render it on the DOM.
1function App() {
2 return (
3 <>
4 <MyDivComponent style={divStyles}/>
5 </>
6 );
7}
This renders a div
with the desired styles from divStyles
. Similarly, all CSS rules can be put together as a JavaScript object and then passed on to the required component as props.
One style object can be used to style different components, or rather different JSX elements within a component. It acts as the CSS stylesheet for that component, and using the default style prop, some CSS properties from that object can be assigned to these elements. This is very similar to the way a component's stylesheet targets different JSX elements using classes.
Add a few more properties to the divStyles
object.
1const divStyles={
2 backgroundColor: 'teal',
3 color:'white',
4 padding: 10,
5 marginLeft: 100,
6 height: 400,
7 width:400
8}
Now allocate separate styles from this object as inline styles to JSX elements.
1const MyDivComponent=({style})=>{
2 console.log(style);
3 return(
4 <>
5 <div style={{'backgroundColor':style.backgroundColor}}>
6 <h2 style={{'color':style.color}}>Default styles in a React component</h2>
7 </div>
8 </>
9 )
10}
Now the component renders with an h2
with a background color of teal and text color of white.
You can also create a separate style object and set it as the default style for some other component, analogous to a global stylesheet. Create the following styles object as an exportable object.
1export const headerStyles={
2 textTransform: 'Uppercase',
3 fontSize: 32,
4 fontWeight:'bold',
5 color:'magenta'
6}
Now headerStyles
can be used in any component. Create a separate component that renders a simple heading with these styles.
1import React from 'react';
2import {headerStyles} from './App'
3const Header=()=>{
4 return(
5 <> <h1 style={headerStyles}>This is header component</h1> </>
6 )
7
8}
9
10export default Header;
Render this component inside your root component.
1function App() {
2 return (
3 <>
4 <Header/>
5 </>
6 );
7}
This way, you can create any number of style objects and either pass them down as props to a component or globally export them so that any component can use them.
Let's look at the advantages and disadvantages of this approach so you can understand when and where to use default styles for your React components.
Styles are described in a declarative manner and are both readable and maintainable.
There are fewer chances of conflicting CSS styles or the need to manually override them.
It iseusable within the component and, in some cases, outside the parent component.
It uses Nodejs to compile at runtime, offering high performance.
The concept of using CSS in JS is fairly young and not entirely accepted by the developer community. Hence, other developers working on your code might find it difficult to change or modify these styles.
It offers less modularity at the document level.
It mostly requires dependencies for compilation, such as jss
, jss-preset-default
, or jss-cli
.
Even though CSS in JS is not widely accepted, using default styles for a React component can sometimes help you avoid messy situations. It also comes in handy when you have to style only a couple of JSX elements or have to create a reusable component with fixed styles. However, due to the well-known drawbacks of inline styling, React apps should only use this approach in combination with the traditional class-based styling approach to style components in order to avoid inconsistency and confusion.