SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
Simple Todo Application With React and Material UI
Hey there everyone, hope you all are doing great. In today’s blog, we are going to create
a simple Todo Application With React and Material UI. We will build this simple React.Js
application and will store our information in the local storage. We will discuss every step
in this blog.
Before we start building this simple application, we assume you have some basic
understanding of JavaScript and the React.Js library. In this application, we will use the
best and most modern approach for our React application. We will separate our code
into smaller chunks so that you easily understand what it’s doing under the hood.
Application Structure of Simple Todo Application with React and Material UI
While building the application we will be keeping the best practices as a developer. We
will divide our app into smaller reusable components and one main file to host our
components. Our application will have three different reusable utility functionalities
folders. They are our context, reducers, and hooks. These folders will have their own
custom functions that will help us in creating logic for our to-do application.
Some Terms To Remember
● Components: Components in React are basically reusable pieces of code that we
can use anywhere in our application. Components can be anything like a reusable
Button, Card, Form, or any stuff that we think can be reused in our application.
The advantage of using the components in React is that it makes our application
more organized and human-friendly.
● Props: So if you are using JavaScript, then you have heard about the term
parameter or say arguments. They can be passed to a function as variable
holders of a function. Similarly, props or properties are the arguments or
parameters that we pass to React Components. Props are read-only. They can’t
mutate directly.
● State: State is the component variable. They hold certain values for our
components that can be changed later on during program execution. States can
be mutated with the help of hooks in React.
It’s Build Time
Now let us start building our application.
The first thing we will do is install Node.Js. Also kindly check the Node.Js version as
well. Now let’s create our project from scratch. But before let me mention that we will
use just React and Material UI.
Installation and Setup
Now let’s create a React App from scratch
npx create-react-app todoApp
or
yarn create react-app todoApp
Once the installation is complete, open this project folder with your favorite code editor.
In this case, we will use the VS Code. Also, let us quickly install the Material UI for our
project. Type the following command in the project root folder in your terminal.
Folder Structure
Our final folder structure will look like this.
node_modules
public
src
|
| --> context
| --> todos.context.js
| --> reducers
| --> todo.reducer.js
| --> hooks
| --> useInputState.js
| --> useLocalStorageReducer.js
| --> useToggleState.js
|
| --> App.js
| --> Todo.js
| --> TodoApp.js
| --> TodoList.js
| --> TodoForm.js
| --> EditTodoForm.js
| --> index.js
| --> index.css
Installing Materail UI
npm install @mui/material @emotion/react @emotion/styled
You can clean the files which you don’t want.
This application requires some logic for CRUD operation. So let’s create our contexts,
reducers, and custom hooks so that we can use them. These utility functions will allow
us to maintain our application state and help us to pass our data dynamically.
Globally Managing our States
Context
At first, we will build our todo context so that we can easily pass our props down the
order where it is necessary. Inside the context folder create a todos.context.js file. This
is the only application context we will create in this entire application.
import React, { createContext } from "react";
import { useLocalStorageReducer } from "../hooks/useLocalStorageReducer";
import todoReducer from "../reducers/todo.reducer";
const defaultTodos = [
{ id: 1, task: "Buy Milks and Bread", completed: false },
{ id: 2, task: "Release ladybugs into garden", completed: true },
];
export const TodosContext = createContext();
export const DispatchContext = createContext();
export function TodosProvider(props) {
const [todos, dispatch] = useLocalStorageReducer(
"todos",
defaultTodos,
todoReducer
);
return (
<TodosContext.Provider value={todos}>
<DispatchContext.Provider value={dispatch}>
{props.children}
</DispatchContext.Provider>
</TodosContext.Provider>
);
}
If you look at the code above, you can see the context logic for our application. We are
passing some default to-do items. The todoReducer will allow us to manipulate the todo
form by creating a todo, editing the todo, and deleting them. Whereas the
useLocalStorageReducer allows us to store our pieces of information.
Hooks
In our custom hooks, we will be creating some custom hooks for managing our
application state that will keep changing at regular intervals. Our application will have
three custom hooks, i.e. ‘useLocalStorageReducer.js’, ‘useInputState.js’, and
‘useToggleState.js’. Each custom hook will have its own use case in our application.
Let us create our first custom hook useInputState.js
import { useState } from "react";
export default initialVal => {
const [value, setValue] = useState(initialVal);
const handleChange = e => {
setValue(e.target.value);
};
const reset = () => {
setValue("");
};
return [value, handleChange, reset];
};
The useInputState.js hook will allow the user to enter the form data of our application.
We can also reset the value of our form input.
Our next custom hook is the useLocalStorageReducer.js. This customs hook provides
us with the capability to store the new or updated to-do items in the local storage of our
web browser.
import { useReducer, useEffect } from "react";
function useLocalStorageReducer(key, defaultVal, reducer) {
const [state, dispatch] = useReducer(reducer, defaultVal, () => {
let value;
try {
value = JSON.parse(
window.localStorage.getItem(key) || String(defaultVal)
);
} catch (e) {
value = defaultVal;
}
return value;
});
useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(state));
}, [key, state]);
return [state, dispatch];
}
export { useLocalStorageReducer };
Our last and final hook is useToggleState.js
jimport { useState } from "react";
function useToggle(initialVal = false) {
// call useState, "reserve piece of state"
const [state, setState] = useState(initialVal);
const toggle = () => {
setState(!state);
};
// return piece of state AND a function to toggle it
return [state, toggle];
}
export default useToggle;
The use of toggle custom hook is that it returns us with the piece of application state
and a function to toggle it.
Reducer
We will use todo.reducer.js it is for our reducer. Note that we are the UUID module to
create a unique id for each of our to-do lists. You can install the UUID with the following
command in your terminal. npm i uuid
import * as uuid from "uuid";
const reducer = (state, action) => {
switch (action.type) {
case "ADD":
return [...state, { id: uuid.v4(), task: action.task, completed: false }];
case "REMOVE":
return state.filter(todo => todo.id !== action.id);
case "TOGGLE":
return state.map(todo =>
todo.id === action.id ? { ...todo, completed: !todo.completed } : todo
);
case "EDIT":
return state.map(todo =>
todo.id === action.id ? { ...todo, task: action.newTask } : todo
);
default:
return state;
}
};
export default reducer;
Building The UI
Now our setup is complete, let’s create our React Component, i.e TodoApp.Js. This will
be our main application component whereas our other component will reside for UI
rendering.
import React from "react";
import TodoList from "./TodoList";
import TodoForm from "./TodoForm";
import Typography from "@material-ui/core/Typography";
import Paper from "@material-ui/core/Paper";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Grid from "@material-ui/core/Grid";
import { TodosProvider } from "./context/todos.context";
function TodoApp() {
return (
<Paper
style={{
padding: 0,
margin: 0,
height: "100vh",
backgroundColor: "#fafafa"
}}
elevation={0}
>
<AppBar color='primary' position='static' style={{ height: "64px" }}>
<Toolbar>
<Typography color='inherit'>React ToDo</Typography>
</Toolbar>
</AppBar>
<Grid container justify='center' style={{ marginTop: "1rem" }}>
<Grid item xs={11} md={8} lg={4}>
<TodosProvider>
<TodoForm />
<TodoList />
</TodosProvider>
</Grid>
</Grid>
</Paper>
);
}
export default TodoApp;
This component will act as a container for our application. You can consider this to-do
component as a Parent element. Our component will display of todo application which
will render the other components.
Todo.js Component
import React, { useContext, memo } from "react";
import useToggleState from "./hooks/useToggleState";
import EditTodoForm from "./EditTodoForm";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItem";
import Checkbox from "@material-ui/core/Checkbox";
import IconButton from "@material-ui/core/IconButton";
import DeleteIcon from "@material-ui/icons/Delete";
import EditIcon from "@material-ui/icons/Edit";
import ListItemSecondaryAction from "@material-ui/core/ListItemSecondaryAction";
import { DispatchContext } from "./context/todos.context";
function Todo({ id, task, completed }) {
const dispatch = useContext(DispatchContext);
const [isEditing, toggle] = useToggleState(false);
// console.log("RE-RENDER ==> ", task);
return (
<ListItem style={{ height: "64px" }}>
{isEditing ? (
<EditTodoForm id={id} task={task} toggleEditForm={toggle} />
) : (
<>
<Checkbox
tabIndex={-1}
checked={completed}
onClick={() => dispatch({ type: "TOGGLE", id: id })}
/>
<ListItemText
style={{ textDecoration: completed ? "line-through" : "none" }}
>
{task}
</ListItemText>
<ListItemSecondaryAction>
<IconButton
aria-label="Delete"
onClick={() => dispatch({ type: "REMOVE", id: id })}
>
<DeleteIcon />
</IconButton>
<IconButton aria-label="Edit" onClick={toggle}>
<EditIcon />
</IconButton>
</ListItemSecondaryAction>
</>
)}
</ListItem>
);
}
export default memo(Todo);
The Todo.js component is our main call to action component where most of the
application logic will reside.
Our next component will be todoList.js
import React, { useContext } from "react";
import Todo from "./Todo";
import Paper from "@material-ui/core/Paper";
import List from "@material-ui/core/List";
import Divider from "@material-ui/core/Divider";
import { TodosContext } from "./context/todos.context";
function TodoList() {
const todos = useContext(TodosContext);
if (todos.length)
return (
<Paper>
<List>
{todos.map((todo, i) => (
// To add a key to a fragment, we have to use the long-hand version
// rather than <> </>, we have to use <React.Fragment>
<React.Fragment key={i}>
<Todo {...todo} key={todo.id} />
{i < todos.length - 1 && <Divider />}
</React.Fragment>
))}
</List>
</Paper>
);
return null;
}
export default TodoList;
Our TodoList component will return the to-do list items that we create. Whenever we
create a new todo item, this react todo application component will render those new
lists.
TodoForm.js component
import React, { useContext } from "react";
import TextField from "@material-ui/core/TextField";
import Paper from "@material-ui/core/Paper";
import useInputState from "./hooks/useInputState";
import { DispatchContext } from "./context/todos.context";
function TodoForm() {
const [value, handleChange, reset] = useInputState("");
const dispatch = useContext(DispatchContext);
return (
<Paper style={{ margin: "1rem 0", padding: "0 1rem" }}>
<form
onSubmit={e => {
e.preventDefault();
dispatch({ type: "ADD", task: value });
reset();
}}
>
<TextField
value={value}
onChange={handleChange}
margin="normal"
label="Add New Todo"
fullWidth
/>
</form>
</Paper>
);
}
export default TodoForm;
This component allows the user to enter values in the to-do form.
Our final todo component is EditTodoForm.js
import React, { useContext } from "react";
import useInputState from "./hooks/useInputState";
import TextField from "@material-ui/core/TextField";
import { DispatchContext } from "./context/todos.context";
function EditTodoForm({ id, task, toggleEditForm }) {
const dispatch = useContext(DispatchContext);
const [value, handleChange, reset] = useInputState(task);
console.log("EDIT FORM RENDER!!!");
return (
<form
onSubmit={e => {
e.preventDefault();
dispatch({ type: "EDIT", id: id, newTask: value });
reset();
toggleEditForm();
}}
style={{ marginLeft: "1rem", width: "50%" }}
>
<TextField
margin="normal"
value={value}
onChange={handleChange}
fullWidth
autoFocus
/>
</form>
);
}
export default EditTodoForm;
This component will allow us to edit the to-do form.
Now at last we will display all the application UI in our main App.js file.
import React from "react";
import TodoApp from "./TodoApp";
function App() {
return <TodoApp />;
}
export default App;
And that’s it, guys. Here we have a fully functional React Todo Application with modern
React practices. And hope you guys will like this simple react application.
How To Run This Project
It’s very simple to run this project. Just follow the steps.
● Make sure you have node.js installed on your system if not then download it first.
● After that, you open the project in your favorite code editor(VS Code is
preferable).
● If you are using Vs Code then, simply open the code editor and open your project.
○ Then open a new terminal (ctrl + shift + ~ ==> will open a new
terminal).
○ run this command `npm install`
○ then `npm start
● If you want to directly run without a code editor, then follow these steps:
○ open a terminal(command prompt or git bash)
○ goto your project root directory(cd folder name)
○ then again type `npm install` and then after installation `npm start`
In case npm gives you any kind of error then you can run the project with the Yarn
package manager instead. Follow these steps:
● Open your windows terminal
● the type `npm i –global yarn`. This will install your yarn package manager
globally.
● After the installation, repeat the same commands from the above but this time
uses yarn instead of npm.
● `yarn install` and then to start `yarn start`.
Conclusion
That’s it for this tutorial and blog. Here in this article, we learned about building a simple
React Todo List application from scratch with custom React Hooks. The application
uses the Material UI for the design. Hope you will like this simple tutorial.

Más contenido relacionado

La actualidad más candente

Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room LibraryReinvently
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop Ahmed rebai
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend TestingNeil Crosby
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesLauren Yew
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Celery의 빛과 그림자
Celery의 빛과 그림자Celery의 빛과 그림자
Celery의 빛과 그림자Minyoung Jeong
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questionsGoa App
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
9. Software Implementation
9. Software Implementation9. Software Implementation
9. Software Implementationghayour abbas
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP TutorialLorna Mitchell
 

La actualidad más candente (20)

React js
React jsReact js
React js
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
ReactJs
ReactJsReactJs
ReactJs
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
Reactjs workshop
Reactjs workshop Reactjs workshop
Reactjs workshop
 
Automated Frontend Testing
Automated Frontend TestingAutomated Frontend Testing
Automated Frontend Testing
 
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin CoroutinesThreading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
Threading Made Easy! A Busy Developer’s Guide to Kotlin Coroutines
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
jQuery
jQueryjQuery
jQuery
 
Introduction to React Native
Introduction to React NativeIntroduction to React Native
Introduction to React Native
 
Celery의 빛과 그림자
Celery의 빛과 그림자Celery의 빛과 그림자
Celery의 빛과 그림자
 
Reactjs
Reactjs Reactjs
Reactjs
 
NodeJS vs Python.pptx
NodeJS vs Python.pptxNodeJS vs Python.pptx
NodeJS vs Python.pptx
 
Angular interview questions
Angular interview questionsAngular interview questions
Angular interview questions
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
WEB DEVELOPMENT USING REACT JS
 WEB DEVELOPMENT USING REACT JS WEB DEVELOPMENT USING REACT JS
WEB DEVELOPMENT USING REACT JS
 
React js
React jsReact js
React js
 
Introduction to C# Programming
Introduction to C# ProgrammingIntroduction to C# Programming
Introduction to C# Programming
 
9. Software Implementation
9. Software Implementation9. Software Implementation
9. Software Implementation
 
Web Services PHP Tutorial
Web Services PHP TutorialWeb Services PHP Tutorial
Web Services PHP Tutorial
 

Similar a React Todo App with Material UI

React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react jsStephieJohn
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react jsBOSC Tech Labs
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react jsdhanushkacnd
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)Chiew Carol
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
 
How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.Techugo
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksKaty Slemon
 
Getting Started with React v16
Getting Started with React v16Getting Started with React v16
Getting Started with React v16Benny Neugebauer
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with ReactNetcetera
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsMatteo Manchi
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basicrafaqathussainc077
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend DevelopersSergio Nakamura
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React PresentationAngela Lehru
 
End to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaEnd to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaBabacar NIANG
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Katy Slemon
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptKaty Slemon
 

Similar a React Todo App with Material UI (20)

React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
Fundamental concepts of react js
Fundamental concepts of react jsFundamental concepts of react js
Fundamental concepts of react js
 
How to create components in react js
How to create components in react jsHow to create components in react js
How to create components in react js
 
Build web apps with react js
Build web apps with react jsBuild web apps with react js
Build web apps with react js
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.How To Integrate Native Android App With React Native.
How To Integrate Native Android App With React Native.
 
Intro react js
Intro react jsIntro react js
Intro react js
 
How to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooksHow to build a react native app with the help of react native hooks
How to build a react native app with the help of react native hooks
 
Getting Started with React v16
Getting Started with React v16Getting Started with React v16
Getting Started with React v16
 
Content-Driven Apps with React
Content-Driven Apps with ReactContent-Driven Apps with React
Content-Driven Apps with React
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
React Basic and Advance || React Basic
React Basic and Advance   || React BasicReact Basic and Advance   || React Basic
React Basic and Advance || React Basic
 
Introduction to React for Frontend Developers
Introduction to React for Frontend DevelopersIntroduction to React for Frontend Developers
Introduction to React for Frontend Developers
 
react-slides.pptx
react-slides.pptxreact-slides.pptx
react-slides.pptx
 
Pluginkla2019 - React Presentation
Pluginkla2019 - React PresentationPluginkla2019 - React Presentation
Pluginkla2019 - React Presentation
 
ReactJS.pptx
ReactJS.pptxReactJS.pptx
ReactJS.pptx
 
End to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux SagaEnd to end todo list app with NestJs - Angular - Redux & Redux Saga
End to end todo list app with NestJs - Angular - Redux & Redux Saga
 
Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...Redux and context api with react native app introduction, use cases, implemen...
Redux and context api with react native app introduction, use cases, implemen...
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
 

Último

Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 

Último (20)

Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 

React Todo App with Material UI

  • 1. Simple Todo Application With React and Material UI Hey there everyone, hope you all are doing great. In today’s blog, we are going to create a simple Todo Application With React and Material UI. We will build this simple React.Js application and will store our information in the local storage. We will discuss every step in this blog. Before we start building this simple application, we assume you have some basic understanding of JavaScript and the React.Js library. In this application, we will use the best and most modern approach for our React application. We will separate our code into smaller chunks so that you easily understand what it’s doing under the hood. Application Structure of Simple Todo Application with React and Material UI While building the application we will be keeping the best practices as a developer. We will divide our app into smaller reusable components and one main file to host our
  • 2. components. Our application will have three different reusable utility functionalities folders. They are our context, reducers, and hooks. These folders will have their own custom functions that will help us in creating logic for our to-do application. Some Terms To Remember ● Components: Components in React are basically reusable pieces of code that we can use anywhere in our application. Components can be anything like a reusable Button, Card, Form, or any stuff that we think can be reused in our application. The advantage of using the components in React is that it makes our application more organized and human-friendly. ● Props: So if you are using JavaScript, then you have heard about the term parameter or say arguments. They can be passed to a function as variable holders of a function. Similarly, props or properties are the arguments or parameters that we pass to React Components. Props are read-only. They can’t mutate directly. ● State: State is the component variable. They hold certain values for our components that can be changed later on during program execution. States can be mutated with the help of hooks in React. It’s Build Time Now let us start building our application. The first thing we will do is install Node.Js. Also kindly check the Node.Js version as well. Now let’s create our project from scratch. But before let me mention that we will use just React and Material UI. Installation and Setup Now let’s create a React App from scratch npx create-react-app todoApp
  • 3. or yarn create react-app todoApp Once the installation is complete, open this project folder with your favorite code editor. In this case, we will use the VS Code. Also, let us quickly install the Material UI for our project. Type the following command in the project root folder in your terminal. Folder Structure Our final folder structure will look like this. node_modules public src | | --> context | --> todos.context.js | --> reducers | --> todo.reducer.js | --> hooks | --> useInputState.js | --> useLocalStorageReducer.js | --> useToggleState.js | | --> App.js | --> Todo.js | --> TodoApp.js | --> TodoList.js | --> TodoForm.js | --> EditTodoForm.js | --> index.js | --> index.css
  • 4. Installing Materail UI npm install @mui/material @emotion/react @emotion/styled You can clean the files which you don’t want. This application requires some logic for CRUD operation. So let’s create our contexts, reducers, and custom hooks so that we can use them. These utility functions will allow us to maintain our application state and help us to pass our data dynamically. Globally Managing our States Context At first, we will build our todo context so that we can easily pass our props down the order where it is necessary. Inside the context folder create a todos.context.js file. This is the only application context we will create in this entire application. import React, { createContext } from "react"; import { useLocalStorageReducer } from "../hooks/useLocalStorageReducer"; import todoReducer from "../reducers/todo.reducer"; const defaultTodos = [ { id: 1, task: "Buy Milks and Bread", completed: false }, { id: 2, task: "Release ladybugs into garden", completed: true },
  • 5. ]; export const TodosContext = createContext(); export const DispatchContext = createContext(); export function TodosProvider(props) { const [todos, dispatch] = useLocalStorageReducer( "todos", defaultTodos, todoReducer ); return ( <TodosContext.Provider value={todos}> <DispatchContext.Provider value={dispatch}> {props.children} </DispatchContext.Provider> </TodosContext.Provider> ); }
  • 6. If you look at the code above, you can see the context logic for our application. We are passing some default to-do items. The todoReducer will allow us to manipulate the todo form by creating a todo, editing the todo, and deleting them. Whereas the useLocalStorageReducer allows us to store our pieces of information. Hooks In our custom hooks, we will be creating some custom hooks for managing our application state that will keep changing at regular intervals. Our application will have three custom hooks, i.e. ‘useLocalStorageReducer.js’, ‘useInputState.js’, and ‘useToggleState.js’. Each custom hook will have its own use case in our application. Let us create our first custom hook useInputState.js import { useState } from "react"; export default initialVal => { const [value, setValue] = useState(initialVal); const handleChange = e => { setValue(e.target.value); }; const reset = () => { setValue(""); }; return [value, handleChange, reset]; };
  • 7. The useInputState.js hook will allow the user to enter the form data of our application. We can also reset the value of our form input. Our next custom hook is the useLocalStorageReducer.js. This customs hook provides us with the capability to store the new or updated to-do items in the local storage of our web browser. import { useReducer, useEffect } from "react"; function useLocalStorageReducer(key, defaultVal, reducer) { const [state, dispatch] = useReducer(reducer, defaultVal, () => { let value; try { value = JSON.parse( window.localStorage.getItem(key) || String(defaultVal) ); } catch (e) { value = defaultVal; } return value; }); useEffect(() => {
  • 8. window.localStorage.setItem(key, JSON.stringify(state)); }, [key, state]); return [state, dispatch]; } export { useLocalStorageReducer }; Our last and final hook is useToggleState.js jimport { useState } from "react"; function useToggle(initialVal = false) { // call useState, "reserve piece of state" const [state, setState] = useState(initialVal); const toggle = () => { setState(!state); }; // return piece of state AND a function to toggle it return [state, toggle]; }
  • 9. export default useToggle; The use of toggle custom hook is that it returns us with the piece of application state and a function to toggle it. Reducer We will use todo.reducer.js it is for our reducer. Note that we are the UUID module to create a unique id for each of our to-do lists. You can install the UUID with the following command in your terminal. npm i uuid import * as uuid from "uuid"; const reducer = (state, action) => { switch (action.type) { case "ADD": return [...state, { id: uuid.v4(), task: action.task, completed: false }]; case "REMOVE": return state.filter(todo => todo.id !== action.id); case "TOGGLE": return state.map(todo => todo.id === action.id ? { ...todo, completed: !todo.completed } : todo ); case "EDIT":
  • 10. return state.map(todo => todo.id === action.id ? { ...todo, task: action.newTask } : todo ); default: return state; } }; export default reducer; Building The UI Now our setup is complete, let’s create our React Component, i.e TodoApp.Js. This will be our main application component whereas our other component will reside for UI rendering. import React from "react"; import TodoList from "./TodoList"; import TodoForm from "./TodoForm"; import Typography from "@material-ui/core/Typography";
  • 11. import Paper from "@material-ui/core/Paper"; import AppBar from "@material-ui/core/AppBar"; import Toolbar from "@material-ui/core/Toolbar"; import Grid from "@material-ui/core/Grid"; import { TodosProvider } from "./context/todos.context"; function TodoApp() { return ( <Paper style={{ padding: 0, margin: 0, height: "100vh", backgroundColor: "#fafafa" }} elevation={0} > <AppBar color='primary' position='static' style={{ height: "64px" }}>
  • 12. <Toolbar> <Typography color='inherit'>React ToDo</Typography> </Toolbar> </AppBar> <Grid container justify='center' style={{ marginTop: "1rem" }}> <Grid item xs={11} md={8} lg={4}> <TodosProvider> <TodoForm /> <TodoList /> </TodosProvider> </Grid> </Grid> </Paper> ); } export default TodoApp;
  • 13. This component will act as a container for our application. You can consider this to-do component as a Parent element. Our component will display of todo application which will render the other components. Todo.js Component import React, { useContext, memo } from "react"; import useToggleState from "./hooks/useToggleState"; import EditTodoForm from "./EditTodoForm"; import ListItem from "@material-ui/core/ListItem"; import ListItemText from "@material-ui/core/ListItem"; import Checkbox from "@material-ui/core/Checkbox"; import IconButton from "@material-ui/core/IconButton"; import DeleteIcon from "@material-ui/icons/Delete"; import EditIcon from "@material-ui/icons/Edit"; import ListItemSecondaryAction from "@material-ui/core/ListItemSecondaryAction"; import { DispatchContext } from "./context/todos.context"; function Todo({ id, task, completed }) { const dispatch = useContext(DispatchContext); const [isEditing, toggle] = useToggleState(false);
  • 14. // console.log("RE-RENDER ==> ", task); return ( <ListItem style={{ height: "64px" }}> {isEditing ? ( <EditTodoForm id={id} task={task} toggleEditForm={toggle} /> ) : ( <> <Checkbox tabIndex={-1} checked={completed} onClick={() => dispatch({ type: "TOGGLE", id: id })} /> <ListItemText style={{ textDecoration: completed ? "line-through" : "none" }} > {task} </ListItemText>
  • 15. <ListItemSecondaryAction> <IconButton aria-label="Delete" onClick={() => dispatch({ type: "REMOVE", id: id })} > <DeleteIcon /> </IconButton> <IconButton aria-label="Edit" onClick={toggle}> <EditIcon /> </IconButton> </ListItemSecondaryAction> </> )} </ListItem> ); } export default memo(Todo);
  • 16. The Todo.js component is our main call to action component where most of the application logic will reside. Our next component will be todoList.js import React, { useContext } from "react"; import Todo from "./Todo"; import Paper from "@material-ui/core/Paper"; import List from "@material-ui/core/List"; import Divider from "@material-ui/core/Divider"; import { TodosContext } from "./context/todos.context"; function TodoList() { const todos = useContext(TodosContext); if (todos.length) return ( <Paper> <List> {todos.map((todo, i) => ( // To add a key to a fragment, we have to use the long-hand version // rather than <> </>, we have to use <React.Fragment>
  • 17. <React.Fragment key={i}> <Todo {...todo} key={todo.id} /> {i < todos.length - 1 && <Divider />} </React.Fragment> ))} </List> </Paper> ); return null; } export default TodoList; Our TodoList component will return the to-do list items that we create. Whenever we create a new todo item, this react todo application component will render those new lists. TodoForm.js component import React, { useContext } from "react"; import TextField from "@material-ui/core/TextField"; import Paper from "@material-ui/core/Paper";
  • 18. import useInputState from "./hooks/useInputState"; import { DispatchContext } from "./context/todos.context"; function TodoForm() { const [value, handleChange, reset] = useInputState(""); const dispatch = useContext(DispatchContext); return ( <Paper style={{ margin: "1rem 0", padding: "0 1rem" }}> <form onSubmit={e => { e.preventDefault(); dispatch({ type: "ADD", task: value }); reset(); }} > <TextField value={value} onChange={handleChange}
  • 19. margin="normal" label="Add New Todo" fullWidth /> </form> </Paper> ); } export default TodoForm; This component allows the user to enter values in the to-do form. Our final todo component is EditTodoForm.js import React, { useContext } from "react"; import useInputState from "./hooks/useInputState"; import TextField from "@material-ui/core/TextField"; import { DispatchContext } from "./context/todos.context"; function EditTodoForm({ id, task, toggleEditForm }) { const dispatch = useContext(DispatchContext);
  • 20. const [value, handleChange, reset] = useInputState(task); console.log("EDIT FORM RENDER!!!"); return ( <form onSubmit={e => { e.preventDefault(); dispatch({ type: "EDIT", id: id, newTask: value }); reset(); toggleEditForm(); }} style={{ marginLeft: "1rem", width: "50%" }} > <TextField margin="normal" value={value} onChange={handleChange} fullWidth
  • 21. autoFocus /> </form> ); } export default EditTodoForm; This component will allow us to edit the to-do form. Now at last we will display all the application UI in our main App.js file. import React from "react"; import TodoApp from "./TodoApp"; function App() { return <TodoApp />; } export default App; And that’s it, guys. Here we have a fully functional React Todo Application with modern React practices. And hope you guys will like this simple react application. How To Run This Project It’s very simple to run this project. Just follow the steps.
  • 22. ● Make sure you have node.js installed on your system if not then download it first. ● After that, you open the project in your favorite code editor(VS Code is preferable). ● If you are using Vs Code then, simply open the code editor and open your project. ○ Then open a new terminal (ctrl + shift + ~ ==> will open a new terminal). ○ run this command `npm install` ○ then `npm start ● If you want to directly run without a code editor, then follow these steps: ○ open a terminal(command prompt or git bash) ○ goto your project root directory(cd folder name) ○ then again type `npm install` and then after installation `npm start` In case npm gives you any kind of error then you can run the project with the Yarn package manager instead. Follow these steps: ● Open your windows terminal ● the type `npm i –global yarn`. This will install your yarn package manager globally. ● After the installation, repeat the same commands from the above but this time uses yarn instead of npm. ● `yarn install` and then to start `yarn start`. Conclusion That’s it for this tutorial and blog. Here in this article, we learned about building a simple React Todo List application from scratch with custom React Hooks. The application uses the Material UI for the design. Hope you will like this simple tutorial.