Reader small image

You're reading from  React and React Native - Fifth Edition

Product typeBook
Published inApr 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781805127307
Edition5th Edition
Languages
Tools
Right arrow
Authors (2):
Mikhail Sakhniuk
Mikhail Sakhniuk
author image
Mikhail Sakhniuk

Mikhail Sakhniuk is Software Engineer with high proficiency in JavaScript, React and React Native. He has more than 5 years of experience in developing web and mobile applications. He has worked for startups, fintech companies, and product companies with more than 20 million users. Currently, Mikhail is working at Miro as a Frontend Engineer. In addition, he owns and maintains a few open-source projects. He also shares his experience and knowledge through books and articles.
Read more about Mikhail Sakhniuk

Adam Boduch
Adam Boduch
author image
Adam Boduch

Adam Boduch has been involved in large-scale JavaScript development for nearly 15 years. Before moving to the frontend, he worked on several large-scale cloud computing products using Python and Linux. No stranger to complexity, Adam has practical experience with real-world software systems and the scaling challenges they pose.
Read more about Adam Boduch

View More author details
Right arrow

User Interface Framework Components

When you are developing a React application, it’s typical to rely on an existing UI library rather than building one from scratch. There are lots of React UI component libraries available to choose from, and there’s no wrong choice, as long as the components make your life simpler.

In this chapter, we delve into the Material UI React library, a popular choice for React development. Material UI stands out due to its comprehensive suite of customizable components, adherence to Google’s Material Design principles, and extensive documentation, making it an optimal choice for developers seeking efficiency and aesthetic coherence in their UI design. Here are the specific topics that we’ll cover:

  • Layout and UI organization
  • Using navigation components
  • Collecting user input
  • Working with styles and themes

Technical requirements

You can find the code files present in this chapter on GitHub at https://github.com/PacktPublishing/React-and-React-Native-5E/tree/main/Chapter09.

You can also find more information about Material UI components and its API at https://mui.com/material-ui/.

Layout and organization

Material UI excels in simplifying the complex process of designing application layouts. By offering a robust set of components, specifically containers and grids, it empowers developers to efficiently structure and organize UI elements. Containers serve as the foundation, providing a flexible way to encapsulate and align content within the overall layout. Grids, on the other hand, allow more granular control, enabling precise placement and alignment of components across different screen sizes, ensuring responsiveness and consistency.

This section aims to unpack the functionality of containers and grids within Material UI. We’ll explore how these tools can be leveraged to create intuitive and aesthetically pleasing layouts, which are crucial for enhancing user experience.

Using containers

Aligning components horizontally on a page often presents a significant challenge due to the intricate balance required between spacing, alignment, and responsiveness...

Using navigation components

Once we have an idea of how the layout of our application is going to look and work, we can start to think about the navigation. This is an important piece of our UI because it’s how the user gets around the application, and it will be used frequently. In this section, we’ll learn about two of the navigational components offered by Material UI.

Navigating with drawers

The Drawer component, just like a physical drawer, slides open to reveal content that is easily accessed. When we’re finished, the drawer closes again. This works well for navigation because it stays out of the way, allowing more space on the screen for the active task that the user is engaged with. Let’s look at an example, starting with the App component:

<BrowserRouter>
  <Button onClick={toggleDrawer}>Open Nav</Button>
  <section>
    <Routes>
      <Route path="/first" element={<First />} />
 ...

Collecting user input

Collecting input from users can be difficult. There are many nuanced things about every field that we need to consider if we plan on getting the user experience right. Thankfully, the Form components available in Material UI take care of a lot of usability concerns for us. In this section, you’ll get a brief sampling of the input controls that you can use.

Checkboxes and radio buttons

Checkboxes are useful for collecting true/false answers from users, while radio buttons are useful for getting the user to select an option from a short number of choices. Let’s take a look at an example of these components in Material UI:

export default function Checkboxes() {
  const [checkbox, setCheckbox] = React.useState(false);
  const [radio, setRadio] = React.useState("First");
  return (
    <div>
      <FormControlLabel
        label={'Checkbox ${checkbox ? "(checked)" : ""}'}
        control={...

Working with styles and themes

Included with Material UI are systems for extending the styles of UI components and extending theme styles that are applied to all components. In this section, you’ll learn about using both of these systems.

Making styles

Material UI comes with a styled() function that can be used to create styled components based on JavaScript objects. The return value of this function is a new component with the new styles applied.

Let’s take a closer look at this approach:

const StyledButton = styled(Button)(({ theme }) => ({
  "&.MuiButton-root": { margin: theme.spacing(1) },
  "&.MuiButton-contained": { borderRadius: 50 },
  "&.MuiButton-sizeSmall": { fontWeight: theme.typography.fontWeightLight },
}));
export default function App() {
  return (
    <>
      <StyledButton>First</StyledButton>
      <StyledButton variant="contained">Second</StyledButton...

Summary

This chapter was a very brief introduction to Material UI, the most popular React UI framework. We started by looking at the components used to assist with the layout of our pages. We then looked at components that can help the user navigate around your application. Next, you learned how to collect user input using Material UI form components. Finally, you learned how to style your Material UI using styles and modifying themes.

The insights gained from this chapter allow you to build complex interfaces without the overhead of developing UI components from scratch, accelerating your development process. Furthermore, React application development inherently relies on the synergistic use of various auxiliary libraries. A deep understanding of the React ecosystem and its key libraries empowers developers to rapidly prototype and iterate on their applications, making development effective.

In the next chapter, we’ll look at ways to improve the efficiency of your...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
React and React Native - Fifth Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781805127307
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Authors (2)

author image
Mikhail Sakhniuk

Mikhail Sakhniuk is Software Engineer with high proficiency in JavaScript, React and React Native. He has more than 5 years of experience in developing web and mobile applications. He has worked for startups, fintech companies, and product companies with more than 20 million users. Currently, Mikhail is working at Miro as a Frontend Engineer. In addition, he owns and maintains a few open-source projects. He also shares his experience and knowledge through books and articles.
Read more about Mikhail Sakhniuk

author image
Adam Boduch

Adam Boduch has been involved in large-scale JavaScript development for nearly 15 years. Before moving to the frontend, he worked on several large-scale cloud computing products using Python and Linux. No stranger to complexity, Adam has practical experience with real-world software systems and the scaling challenges they pose.
Read more about Adam Boduch