CodeNewbie Community 🌱

Sebastian9995
Sebastian9995

Posted on

Different ways to design apps in React

Every front-end developer knows about the normal ways to design a web application. Connect external stylesheet with an HTML file, apply classes and that’s it. However, many front-end developers don’t know that there are many other ways to customize the appearance of a web application built in React.

Design web application via external stylesheet

This is the standard approach for styling web applications in CSS. Linking React components with CSS files is pretty similar to how we do it in HTML. First we create a CSS stylesheet, write required CSS rules, and use the import statement to make it available in the React component.

Linking CSS files with React components is good if you’re already familiar with the process of writing CSS rules for styling web applications. Most web developers have been using this syntax for years.

Using inline styles

Many React developers will advise against using inline styles, but there are certain use cases where they are useful. For example, if you are building a prototype of a web application, or just a small application.

In React, inline styles are written as JavaScript objects. And you probably know that React elements are written in JSX. You can set the style attribute of React elements to a JavaScript objects where keys represent CSS properties, and values represent CSS values. For example, something like this:

{backgroundColor: “red”}

As you can see, instead of writing CSS property as you would in a separate stylesheet (‘background-color’), we write it as backgroundColor – two words pushed together and camelCased. Also, the value of this property needs to be a string or other valid JavaScript value. You can also use string interpolation to dynamically style element in React. Here's a guide that explains how that works:

https://simplefrontend.com/string-interpolation-in-react-tutorial-with-examples/

One final detail is that in order to evaluate JavaScript objects as such inside JSX, you need to wrap JavaScript code with an additional pair of curly braces.

Conditional styling

A great thing about React web applications is that you can dynamically customize the appearance of each element. For example, if a user enters wrong value into the email field, change the color of input element to red and highlight the error.

In React, you can use expressions like ternary operators to set up a condition and dynamically return one class value or another. This is the best approach for setting an inline expression that generates a dynamic class value. The process is better described in this tutorial on setting conditional className values in React.
Alternatively, you can create a condition outside of JSX. This is better, because you can’t use statements like if, case or a for loop within JSX. You can set a much more complex condition outside of it.

External libraries

React community has created various libraries for handling common styling tasks in React. For instance, the classnames package provides a function of the same name, used for dynamically applying class values.
There is also a popular styled-components package, which can help you create local CSS rules for components. It is commonly used in development of large scale web applications. You will often find that the knowledge of this package is necessary to get a job.

Top comments (5)

Collapse
 
alexanderjohnes34 profile image
alexanderjohn

Great overview of different styling methods in React! Linking external stylesheets is indeed a common approach, especially for developers familiar with traditional CSS workflows. It's great to explore alternative methods to customize the appearance of React applications. Looking forward to ob whatsapp android more insights on advanced styling techniques.

Collapse
 
jamesjalasko3434 profile image
james jalsko

Designing apps in React involves structuring and organizing components, managing state and props, handling user interactions, and ensuring a smooth user experience. There are several approaches to designing apps in React, each with its own benefits and use cases. Here are some different ways to design apps in React to the calcul temps de travail

Collapse
 
henry74 profile image
henry

In React, apps can be designed using various approaches, such as functional components, class components, and hooks like useState and useEffect. Additionally, libraries like Redux can be used for managing state in larger applications. Design patterns like Container/Component and Presentational/Container patterns help organize code. CSS-in-JS solutions like styled components or CSS modules can style in lavaloungehouston.com/ components. Lastly, React Router enables navigation and routing in single-page applications.

Collapse
 
jackiee32 profile image
Rana Arham

Discover the future of railways travel. Our website train speed meter is dedicated to providing real-time insights into train speeds and performance. Whether you're a commuter, enthusiast, or industry professional, stay informed about the latest advancements and speed data. Train Speed Test is your one-stop destination for all things related to the velocity and efficiency of trains.

Collapse
 
dingadu52 profile image
dingadu52

Discover the epitome of Marietta hardwood floor installation expertise right at your fingertips with GM Remodeling Services' dedicated website. Our online platform serves as your gateway to a world of premium hardwood flooring options, expert advice, and seamless project coordination. Browse through our extensive gallery to envision the transformative possibilities for your Marietta home, from classic oak to exotic hardwoods, all showcased with stunning clarity. With user-friendly navigation and detailed information about our services, pricing, and testimonials, our website is designed to empower you to make informed decisions about your hardwood floor installation project. Trust GM Remodeling Services to bring your Marietta home renovation dreams to life, starting with a visit to our website today.