Reactjs Material Ui Login Page

Web Development Software

ReactJS Material UI Login Page

As a developer with a passion for creating visually appealing and user-friendly interfaces, one of the tools in my arsenal that I love to use is ReactJS. Combining the power of ReactJS with the elegance of Material UI, I am able to create stunning login pages that not only provide a secure entrance for users but also leave a lasting impression. In this article, I will take you through the process of creating a ReactJS Material UI login page, sharing my personal touches and commentary along the way.

Getting Started

Before diving into the technical details, let’s take a moment to appreciate the beauty of Material UI. Material UI is a popular ReactJS component library that follows Google’s Material Design guidelines. It provides a wide range of ready-to-use components, including buttons, text fields, and cards, that can be easily customized to fit your application’s look and feel.

To begin, we need to set up a new ReactJS project. If you haven’t already installed Node.js and create-react-app, make sure to do so before proceeding. Once everything is set up, open your terminal and navigate to your desired project directory. Run the following command to create a new React app:

npx create-react-app my-login-page

Once the project is created, navigate to the project directory by running the following command:

cd my-login-page

Now, we can install Material UI by running the following command:

npm install @material-ui/core

With Material UI installed, we are ready to start building our login page.

Building the Login Form

The foundation of any login page is the login form itself. Material UI provides a TextField component that we can use to create the input fields for the username and password. Let’s start by importing the necessary components and creating a basic form structure:

{`
import React, { useState } from 'react';
import { TextField, Button } from '@material-ui/core';

const LoginForm = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');

const handleUsernameChange = (event) => {
setUsername(event.target.value);
};

const handlePasswordChange = (event) => {
setPassword(event.target.value);
};

const handleLogin = () => {
// TODO: Handle login functionality
};

return (




);
};

export default LoginForm;
`}

In the code snippet above, we create a functional component called LoginForm. Inside this component, we use the useState hook to manage the state of the username and password fields. We also define two event handlers: handleUsernameChange and handlePasswordChange, which update the state whenever the user types in the input fields.

The TextField component is used to create the input fields. We provide a label prop to specify the text to be displayed above each field. The value prop is used to bind the field to the corresponding state variable, and the onChange prop is used to update the state whenever the user types in the field.

The Button component is used to create the login button. We provide variant=”contained” and color=”primary” props to style the button according to Material UI’s guidelines. We also pass the handleLogin function to the onClick prop, which will be invoked when the user clicks on the button. However, for now, the handleLogin function is empty.

Before we move on, let’s import and use the LoginForm component in our App.js file:

{`
import React from 'react';
import LoginForm from './LoginForm';

const App = () => {
return (

Welcome to My Login Page

);
};

export default App;
`}

Save the changes and run npm start in your terminal to start the development server. You should see the login form rendered in your browser.

Styling the Login Page

Now that we have the basic form structure in place, let’s add some styling to make our login page more visually appealing. Material UI provides various ways to customize the appearance of components. We can use CSS-in-JS approach provided by Material UI’s makeStyles hook.

{`
import React, { useState } from 'react';
import { TextField, Button, makeStyles } from '@material-ui/core';

const useStyles = makeStyles((theme) => ({
form: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
margin: 'auto',
width: '300px',
padding: '20px',
borderRadius: '5px',
boxShadow: '0 2px 5px rgba(0, 0, 0, 0.3)',
},
textField: {
marginBottom: theme.spacing(2),
width: '100%',
},
loginButton: {
width: '100%',
},
}));

const LoginForm = () => {
const classes = useStyles();
// Rest of the code remains the same...
};
`}

In the code snippet above, we define a useStyles function using the makeStyles hook. Inside this function, we define CSS styles for our form, text fields, and login button. We use the theme object provided by Material UI to access the spacing utility function (theme.spacing) and apply the appropriate margin and padding values.

In the LoginForm component, we assign the return value of useStyles to the classes constant. We can then use these classes in our JSX to apply the defined styles. For example, we apply the form class to the form element, the textField class to the TextField components, and the loginButton class to the Button component.

Save the changes and check your browser. You should see the login form styled according to the CSS rules defined in the useStyles function.

Adding Form Validation

Now that our login form looks great, it’s time to add some form validation to ensure that the user enters valid credentials. Material UI provides a Formik library that makes form validation and handling user input easy.

To install Formik, run the following command:

npm install formik

Once installed, we can import the necessary components and use Formik for our login form:

{`
import { Formik, Form, Field, ErrorMessage } from 'formik';

const LoginForm = () => {
// Rest of the code remains the same...

const validate = (values) => {
const errors = {};

if (!values.username) {
errors.username = 'Required';
}

if (!values.password) {
errors.password = 'Required';
}

return errors;
};

return (
{
// TODO: Handle login functionality
setSubmitting(false);
}}
>









);
};
`}

In the updated code snippet above, we import the necessary components from Formik: Formik, Form, Field, and ErrorMessage.

We define a validate function that takes the form values as an argument and returns an object with validation errors. In this case, we check if the username and password fields are empty and return an error message if they are.

The initialValues prop of the Formik component is set to an object with empty values for the username and password fields. The validate prop is set to our validate function, and the onSubmit prop is set to a function that will handle the form submission. For now, we just set setSubmitting(false) to indicate that the form submission is complete.

Inside the Form component, we replace the TextField components with Field components, passing the respective name and label props. The as prop is used to specify the underlying component to render, which in this case is TextField. We also use ErrorMessage components to display any validation errors.

Save the changes and check your browser. Try submitting the form with empty fields, and you should see the validation errors displayed below the respective input fields.

Conclusion

In this article, we explored the process of creating a ReactJS Material UI login page. We started by setting up a new React app and installing Material UI. We then built the login form using the TextField and Button components provided by Material UI. We added styling using the makeStyles hook and applied CSS rules to customize the appearance of the form. Furthermore, we incorporated form validation using the Formik library to ensure that the user enters valid credentials. With the help of Material UI, we were able to create a visually appealing and user-friendly login page that can be easily customized to fit the look and feel of any application.

Now that you have the knowledge and skills to create a ReactJS Material UI login page, it’s time to take your projects to the next level. Happy coding!

Note: This article is for educational purposes only. Always ensure that you have the necessary legal and ethical permissions before implementing a login system in your application.