In this article, we are going to learn how to create a basic notes app using ReactJS. A notes app is a digital application that allows users to create, manage, and store textual notes for personal organization and reference.
Here is a demonstration of the making of a basic notes app using ReactJs. It will look like this:
Prerequisites:
Project Setup:
Step 1: Create a new React app
npx create-react-app notes-app
Step 2: Change your directory and enter your main folder notes-app as :
cd notes-app
Step 3: Start editting app.js file
Step 4: Check Dependenncies/ package.json:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Project structure:
Approach:
- First start creating a basic notes app layout with dummy text, divs, buttons, input, and heading tags along with well-defined classes and id.
- Use classes and id to style the components using CSS and create a dummy notes app structure as shown in the introduction.
- In react, we use hooks like useState to store the data items and also handle the inputs.
- Create a dummy data array of objects having key, title, and description fields.
- Define a handle function to get the input data and store it using useState hook. Also, define a remove function that accepts the key of the item which is to be removed.
- Use the onClick event listener to call the adding or removing functions and update the notes array.
Example: In this example, we are using the above-explained approach.
Javascript
// App.js   import "./App.css" ; import { useState } from "react" ;   function App() {     const [title, setTitle] = useState( "" );     const [des, setDes] = useState( "" );     const [notes, setNotes] = useState(data);     const [count, setCount] = useState(4);       function remove(id) {         setNotes(notes.filter((e) => e.key !== id));     }       function handle() {         if (!title || !des) {             window.alert( "Incomplete input" );             return ;         }         setNotes([             ...notes,             { key: count, title: title, des: des },         ]);         setCount(count + 1);         setTitle( "" );         setDes( "" );         console.log(notes);     }       return (         <div className= "App" >             <div className= "card" >                 <div className= "head" >                     <h1>React notes</h1>                 </div>                 <div className= "notes" >                     {notes.map((e) => (                         <div className= "notes-item" >                             <div style={{ width: "90%" }}>                                 <h4>Title: {e.title}</h4>                                 <p>Note: {e.des}</p>                             </div>                             <button                                 type= "input"                                 style={{                                     fontSize: "20px" ,                                     width: "8%" ,                                     height: "35px" ,                                     padding: "0 2% 0 2%" ,                                     color: "black" ,                                 }}                                 onClick={() =>                                     remove(e.key)                                 }>                                 X                             </button>                         </div>                     ))}                     <div className= "add" >                         <h3>Add Notes</h3>                         <input                             type= "text"                             id= "title"                             placeHolder= "Add title"                             value={title}                             onChange={(e) =>                                 setTitle(e.target.value)                             }>                           </input>                         <input                             type= "text"                             id= "description"                             placeholder= "Notes"                             value={des}                             onChange={(e) => {                                 setDes(e.target.value);                             }}>                           </input>                         <button                             type= "submit"                             onClick={handle}>                             Submit                         </button>                     </div>                 </div>             </div>         </div>     ); }   // Dummy data const data = [     {         key: 0,         title: "Html" ,         des: "HyperText MarkUp Language" ,     },     { key: 1, title: "CSS" , des: "StyleSheet" },     {         key: 2,         title: "JavaScript" ,         des: "Scripting language for web" ,     },     {         key: 3,         title: "React" ,         des: "JavaScript framework" ,     }, ];   export default App; |
CSS
/* App.css*/ Â Â .App { Â Â Â Â text-align : center ; } Â Â /* Styling universal selector */ * { Â Â Â Â /* margin-top: 50px; */ Â Â Â Â margin : 0 ; Â Â Â Â margin-top : 10px ; Â Â Â Â Â Â padding : 0 ; Â Â Â Â box-sizing: border-box; } Â Â /* Style body element */ body { Â Â Â Â min-height : 50 vh; Â Â Â Â display : flex; Â Â Â Â align-items: center ; Â Â Â Â text-align : center ; Â Â Â Â justify- content : center ; Â Â Â Â background : hsl( 0 , 0% , 100% ); Â Â Â Â font-family : "Poppins" , sans-serif ; } Â Â /* Styling card container */ .card { Â Â Â Â /* min-height: 50vh; */ Â Â Â Â height : fit-content; Â Â Â Â min-width : 33 rem; Â Â Â Â max-width : 40 rem; Â Â Â Â background : rgba( 147 , 208 , 119 , 1 ); Â Â Â Â margin : 0 1 rem; Â Â Â Â box-shadow: 0 0 5px rgba( 0 , 0 , 0 , 0.2 ); Â Â Â Â width : 100% ; } Â Â .head { Â Â Â Â position : relative ; Â Â Â Â max-height : fit-content; Â Â Â Â min-width : 33 rem; Â Â Â Â width : 100% ; Â Â Â Â background : rgb ( 109 , 157 , 87 ); Â Â Â Â Â Â padding : 1 rem; } Â Â Â Â /* Input box and button */ .add { Â Â Â Â padding : 5% ; Â Â Â Â text-align : left ; Â Â Â Â padding-left : 6% ; Â Â Â Â /* margin: 10%; */ } Â Â input { Â Â Â Â margin-right : 1% ; } Â Â #title { Â Â Â Â width : 20% ; Â Â Â Â font-size : larger ; } Â Â #description { Â Â Â Â font-size : large ; Â Â Â Â width : 60% ; } Â Â button { Â Â Â Â border-radius: 5px ; Â Â Â Â padding : 5px ; Â Â Â Â font-size : large ; Â Â Â Â color : #dbeefa ; Â Â Â Â background-color : rgb ( 109 , 157 , 87 ); } Â Â /* style accordion */ .notes { Â Â Â Â margin : 5% ; Â Â Â Â text-align : left ; } Â Â /* style for accordion items */ .notes-item { Â Â Â Â display : flex; Â Â Â Â z-index : 100 ; Â Â Â Â padding : 2% ; Â Â Â Â /* border-bottom: 1px solid #4e7b48; */ Â Â Â Â font-size : larger ; Â Â Â Â margin : 2% ; Â Â Â Â box-shadow: 0 0 10px rgba( 0 , 0 , 0 , 0.2 ); } Â Â /* style for accordion items */ .accordion-item { Â Â Â Â border-bottom : 1px solid #ddd ; Â Â Â Â font-size : larger ; } |
Step 5: Run the following command and the output will be visible at http://localhost:3000/
npm start
Output: