Open In App

React MUI FormHelperText API

MUI or 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  FormHelperText API. The FormHelperText helps to add an additional text related to the input field in the form for the users.



Import FormHelperText API:

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

 



FormHelperText Props:

CSS Rules:

Syntax:

<FormHelperText></FormHelperText>

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the required module using the following command:

npm install @mui/material 
npm install @emotion/react 
npm install @emotion/styled 

Project Structure: It will look like the following.

 

Example 1: In this example, we added two input fields and also added FormHelperText along with it. To the first FormHelperText component, we are passing id = “username-helper” and to the second one, we added id = “country-helper” and also added the disabled prop.

App.js




import { FormHelperText, FormControl, Input } from '@mui/material';
  
export default function App() {
    return (
        <div style={{ margin: 30 }}>
            <h1 style={{ color: "green" }}>GeeksforGeeks</h1>
            <h4> React MUI  FormHelperText API </h4>
            <FormControl>
                <label>Username :</label>
                <Input id="user" />
                <FormHelperText id="username-helper">
                    must be more than 5 characters
                </FormHelperText>
                <label>Country :</label>
                <Input id="country" disabled />
                <FormHelperText id="country-helper" disabled >
                    disabled field
                </FormHelperText>
            </FormControl>
        </div>
    );
}

Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output.

 

Example 2: In this example, we are creating three states using react hook useState naming val, selectBool, and helper text. We are adding a function named onHandleChange that checks the length of the current input string and changes the states. To the FormHelperText component, we are passing selectBool to the error prop. When we are typing anything in the input field the on-change function onHandleChange gets triggered.

App.js




import { useState } from 'react';
import { FormHelperText, FormControl, Input }
    from '@mui/material';
  
export default function App() {
    const [val, setVal] = useState("")
    const [selectBool, setSelectBool] = useState(false);
    const [helperText, setHelperText] =
        useState(" username must be more than 9 characters")
  
    const onHandleChange = (e) => {
        setVal(e.target.value);
        if (val.length > 9) {
            setSelectBool(false)
            setHelperText("correct")
        }
        else {
            setSelectBool(true)
            setHelperText(
                "error: must be more than 9 characters")
        }
    };
    return (
        <div style={{ margin: 30 }}>
            <h1 style={{ color: "green" }}>GeeksforGeeks</h1>
            <h4> React MUI FormHelperText API </h4>
            <FormControl>
                <label>Username :</label>
                <Input id="user" onChange={onHandleChange}
                    value={val} />
                <FormHelperText id="username-helper"
                    error={selectBool}>{helperText}
                </FormHelperText>
            </FormControl>
        </div>
    );
}

Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output.

 

Reference: https://mui.com/material-ui/api/form-helper-text/


Article Tags :