Open In App

React MUI Stack API

Last Updated : 01 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google. 

In this article, we will discuss the React MUI Stack API. The Stack component is used to manage the immediate children on the horizontal or vertical axis with optional spacing and padding between them. The API provides a lot of functionality and we will learn to implement them.

Import Stack API:

import Stack from '@mui/material/Stack';
// or
import { Stack } from '@mui/material';

Props List: Here is the list of props used with this component. We can access them and modify them according to our needs.

  • children (node): It is the content of the component.
  • component (elementType): It is the component for the root node.
  • sx (Array<func/object/bool> func/object): The system prop allows defining system overrides as well as additional CSS styles. 
  • direction (column-reverse/column/row-reverse/row/array): It defines the flex-direction. The default value is the column.
  • divider (node): Adds a divider between children. 
  • spacing (Array<number/string>/number/object/string): It specifies the spacing between the children. The default value is 0.

CSS Rules: All CSS utilities are supported.

Syntax: Create a Stack element as follows:

<Stack spacing={2}>
      <Item>Tutorial 1</Item>
      <Item>Tutorial 2</Item>
      <Item>Tutorial 3</Item>
</Stack>

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab @mui/icons-material

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a Stack with some items.

App.js

Javascript




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Paper from "@mui/material/Paper";
import Stack from "@mui/material/Stack";
import { styled } from "@mui/material/styles";
  
const Item = styled(Paper)(({ theme }) => ({
    backgroundColor: theme.palette
        .mode === "dark" ? "#1A2027" : "#fff",
    ...theme.typography.body2,
    padding: theme.spacing(1),
    textAlign: "center",
    color: theme.palette.text.secondary,
}));
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Stack API</strong>
            </div>
            <br />
            <Box sx={{ width: "50%", margin: "auto" }}>
                <Stack spacing={2}>
                    <Item>Data Structures</Item>
                    <Item>Algorithms</Item>
                    <Item>Web Development</Item>
                </Stack>
            </Box>
        </div>
    );
}
  
export default App;


Output:

 

Example 2: In the following example, we have changed from column to row.

App.js

Javascript




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Paper from "@mui/material/Paper";
import Stack from "@mui/material/Stack";
import { styled } from "@mui/material/styles";
  
const Item = styled(Paper)(({ theme }) => ({
    backgroundColor: theme.palette
        .mode === "dark" ? "#1A2027" : "#fff",
    ...theme.typography.body2,
    padding: theme.spacing(1),
    textAlign: "center",
    color: theme.palette.text.secondary,
}));
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI Stack API</strong>
            </div>
            <br />
            <Box sx={{ 
                width: "fit-content"
                margin: "auto" 
            }}>
                <Stack direction="row" spacing={2}>
                    <Item>Data Structures</Item>
                    <Item>Algorithms</Item>
                    <Item>Web Development</Item>
                </Stack>
            </Box>
        </div>
    );
}
  
export default App;


Output

 

Reference: https://mui.com/material-ui/api/stack/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads