Open In App

React MUI TextField API

React MUI 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 are going to discuss the React MUI TextField API. TextField allows us to enter the text in form fields like name, address, etc. The TextField API exposes many features that can be used to make our TextField even more helpful and dynamic. 



Import TextField API:

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

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



Syntax: Create a TextField as follows:

<TextField id="name" label="Standard" variant="standard" />

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

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a basic TextField to enter the name.




import * as React from 'react'
import { TextField } from '@mui/material'
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: 'fit-content',
                    margin: 'auto',
                }}
            >
                <h1
                    style={{
                        color: 'green',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TextField API</strong>
                <br />
                <TextField
                    id="name"
                    label="Name"
                    variant="outlined"
                    placeholder="Enter your name"
                />
            </div>
        </div>
    )
}
  
export default App

Output:

 

Example 2: In the following example, we have TextField which is required and has validation.




import * as React from 'react'
import { TextField } from '@mui/material'
import { useState } from 'react'
function App() {
    const [error, setError] = useState(true)
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: 'fit-content',
                    margin: 'auto',
                }}
            >
                <h1
                    style={{
                        color: 'green',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TextField API</strong>
                <br />
                <TextField
                    required
                    error={error}
                    onChange={() => {
                        console.log(document.getElementById('name').value)
                        document.getElementById('name').value.length < 5
                            ? setError(true)
                            : setError(false)
                    }}
                    helperText={error ? 'Name should be greater than 5' : ''}
                    id="name"
                    label="Name"
                    variant="outlined"
                    placeholder="Enter your name"
                />
            </div>
        </div>
    )
}
  
export default App

Output:

 

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


Article Tags :