K O logo
Web Development

Project WebApp — How to style react

Author

Kalle

Date Published

This AI-generated image depicts a vibrant and meticulously arranged workspace with a strong focus on design and coding. The centerpiece is a tablet displaying a colorful, layered 3D design with coding elements, surrounded by various items such as a custom rainbow-colored keyboard, a smartphone showing a color wheel, and an open notebook with design sketches. The desk is adorned with color palettes, pencils, sticky notes, and other creative tools, all organized in a visually appealing manner. The background features a soft gradient of pink and purple, enhancing the colorful and creative atmosphere. The React.js logo appears prominently on several items, emphasizing the theme of web development and design. The overall scene exudes creativity, organization, and a passion for both coding and design.

The classic approach to styling in the browser is creating a CSS file and provide it with a <link /> tag. The tutorial from last session also worked with an external CSS file, but linked it by adding a JS import statement. The benefits of this way are an easy separation of concerns and the already known workflow of styling HTML elements.

But the separation of concerns also leads to another path. In react an application is separated into components, but the workflow of defining global style-classes is quite contrary. So why not rethink it?

Christiano Rastelli made this wonderful graphic, which shows a new way of drawing boundaries in a component oriented application architecture.

The image contrasts two perspectives on "Separation of Concerns" in web development. On the left, it shows a traditional separation with distinct layers: JS (blue), CSS (green), and HTML (red). On the right, it depicts a blended approach where each component (e.g., Button, Modal) integrates all three technologies, represented by vertical bars with gradients of blue, green, and red.

By separating the architecture in this way, a component becomes responsible for its behavior (JS), layout (HTML) and design (CSS). All these concerns should only be influenced by the component itself.

One viable solution is to use normal CSS rules with unique style rules for every component. For very small projects this approach is well suited but as soon as a project becomes a bit larger and the component count rises it gets harder and harder to name the CSS rules in its global namespace. To eliminate this downside of CSS the simplest solution is to just use inline styling.

1const Title = (props) => (
2 <h1
3 style={{
4 fontSize: "1.5em",
5 textAlign: "center",
6 color: "forestgreen",
7 }}
8 {...props}
9 />
10);

It is simple, but some important features are missing (for example media-queries and keyframe animations) and it's not as performant as css-class-rules.

The solution: Take the best from both worlds!

Write the style rules in JavaScript, but generate class-rules. The most commonly used library to support this workflow is styled-components 💅, which enables you to define components, only containing information about the element type and styling. Those are very well suited as the basic building blocks of any web app. To work with, the styled component lib provides a tagged template literals factory for each HTML base element.

1import styled from "styled-components";
2
3const Title = styled.h1`
4 font-size: 1.5em;
5 text-align: center;
6 color: forestgreen;
7`;

It's even possible to generate style depending on the component props by inserting functions into the template string.

1import styled from "styled-components";
2
3const Title = styled.h1`
4 font-size: 1.5em;
5 text-align: center;
6 color: forestgreen;
7
8 ${(props) =>
9 props.primary &&
10 css`
11 background: forestgreen;
12 color: white;
13 `};
14`;

This string based generation of styles has the benefit of common syntax, if you know CSS. But in comparison to writing simple objects it can get cumbersome if you are working with many variables.

Like always in the JavaScript world, if there is a problem, there will be a lib. For this case glamorous 💄 delivers. It has an API quite similar to styled-components but instead of working with template-strings you define the styles like the react inline styles as objects. For more complex scenarios the component generator function can take multiple arguments, either a style object or a function returning a style object.

1import glamorous from "glamorous";
2
3const Title = glamorous.h1(
4 {
5 fontSize: "1.5em",
6 textAlign: "center",
7 color: "forestgreen",
8 },
9 (props) =>
10 props.primary && {
11 background: "forestgreen",
12 color: "white",
13 },
14);

Last but not least emotion should be mentioned. This lib enables you to work with both template-strings and objects.

1import styled from "react-emotion";
2
3const Title = styled("h1")`
4 font-size: 1.5em;
5 text-align: center;
6 color: ${(props) => (props.primary ? "white" : "forestgreen")} forestgreen;
7 background: ${(props) => (props.primary ? "forestgreen" : "unset")};
8`;
1import styled from "react-emotion";
2
3const Title = styled("h1")(
4 {
5 fontSize: "1.5em",
6 textAlign: "center",
7 color: "forestgreen",
8 },
9 (props) =>
10 props.primary && {
11 background: "forestgreen",
12 color: "white",
13 },
14);

Tasks

 This AI-generated image features a visually striking and meticulously organized workspace with a focus on React.js and Redux development. The centerpiece is a large monitor displaying a colorful, layered data flow diagram, accompanied by lines of code. The desk is filled with items like a Rubik's cube, sticky notes, a coffee mug with the React logo, and a tablet showing additional code. The background wall is covered with diagrams, flowcharts, and keywords such as "Actions," "Reducer," "State Management," and "Redux," all related to the state management concepts in React and Redux. The lighting is a mix of cool and warm tones, creating a dynamic atmosphere that emphasizes productivity and creativity in web development. The overall scene is both educational and aesthetically pleasing, reflecting a deep engagement with coding and software design.

Project WebApp — State management in a react app

Learn to manage state in React using built-in state APIs, focusing on immutability and single sources of truth. This guide covers setState basics, best practices, and introduces Redux for complex state management with practical examples

Mailing List

If you want to receive updates on new posts, leave your email below.