Update Notes in react js

share link

by vsasikalabe dot icon Updated: Apr 4, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Notes are comments or feedback written by users on an edit and are important information or a form of communication between users on the site. They can either be added to an existing edit or can be entered at the same time as an edit. This can happen when props or state changes. This can also happen when forceUpdate is called. A component update may not trigger a DOM update sometimes. This is because React creates a virtual DOM after the update and checks with the virtual DOM before the update. And only if there is a difference in notes is the DOM updated. 


Material UI is an open-source React component library used here to update the notes. It includes a comprehensive collection of pre-in-built components.  

Basic layout and template for a single note that follows below: 

  • The note body: It Contains the user input text to write the notes 
  • The note footer: Contains the delete icon 

Next, create a new CSS file called Style.css, which will contain the styles for the notes. Now, we’ll create a new file called Notes.jsx within the Components folder. Notes.jsx will contain all the states and functions and link together all the components. Inside Notes.jsx. If the user clicks on the delete icon, we will run the delete note function, removing the note from the array by filtering it out. 


Here is an example of how to update Notes in Reactjs: 

Preview of the output that you will get on running this code from your IDE.

Code:

In this solution we use the React library.

import React, { useState } from "react";
// import Header from "./Header";
// import Footer from "./Footer";
import Note from "./Note";
import CreateArea from "./CreateArea";

function App() {
  const [notes, setNotes] = useState([]);

  function addNote(newNote) {
    setNotes((prevNotes) => {
      return [...prevNotes, newNote];
    });
  }
  function editNote(id,title,content){
    const tobeUpdated = notes.find(singleNote => singleNote.id === id)
    tobeUpdated.id = id
    tobeUpdated.title = title;
    tobeUpdated.content = content;
    setNotes([...notes])

    
  }
  function deleteNote(id) {
    setNotes((prevNotes) => {
      return prevNotes.filter((noteItem) => {
        return noteItem.id !== id;
      });
    });
  }
  console.log(notes);
  return (
    <div>
      <CreateArea onAdd={addNote} />
      {notes.map((noteItem) => {
        return (
          <Note
            key={noteItem.id}
            id={noteItem.id}
            title={noteItem.title}
            content={noteItem.content}
            onDelete={deleteNote}
            onEdit={editNote}
          />
        );
      })}
    </div>
  );
}

export default App;



import React, { useState } from "react";
// //import AddIcon from "@material-ui/icons/Add";
// import Fab from "@material-ui/core/Fab";
// import Zoom from "@material-ui/core/Zoom";

function CreateArea(props) {
  const [isExpanded, setExpanded] = useState(false);

  const [note, setNote] = useState({
    id: "",
    title: "",
    content: ""
  });

  function handleChange(event) {
    const { name, value } = event.target;

    setNote((prevNote) => {
      return {
        ...prevNote,
        [name]: value
      };
    });
  }

  function submitNote(event) {
    event.preventDefault();
    setNote({
      id: Math.floor(Math.random() * 100),
      title: "",
      content: ""
    });
    props.onAdd(note);
  }

  function expand() {
    setExpanded(true);
  }

  return (
    <div>
      <form className="create-note">
        {isExpanded && (
          <input
            name="title"
            onChange={handleChange}
            value={note.title}
            placeholder="Title"
          />
        )}

        <textarea
          name="content"
          onClick={expand}
          onChange={handleChange}
          value={note.content}
          placeholder="Take a note..."
          rows={isExpanded ? 3 : 1}
        />
        <button onClick={submitNote}>Submit</button>
      </form>
    </div>
  );
}

export default CreateArea;


import React, { useState } from "react";
//import DeleteIcon from "@material-ui/icons/Delete";
import "./styles.css";

function Note(props) {
  const [displayForm, setForm] = useState(false);
  function handleClick() {
    props.onDelete(props.id);
  }
  function handleEdit(e) {
    e.preventDefault();
    const title = e.target.children[0].value;
    const content = e.target.children[1].value;
    props.onEdit(props.id, title, content);
  
    setForm(false)
  }

  return (
    <div className="note">
      <h1>{props.title}</h1>
      <p>{props.content}</p>
      <form
        onSubmit={handleEdit}
        className={`${displayForm ? "show" : "hide"}`}
      >
        <input placeholder="title" />
        <input placeholder="content" />
        <input type="submit" />
      </form>
      <button onClick={handleClick}>X</button>
      <button onClick={() => setForm(!displayForm)}>Edit</button>
    </div>
  );
}

export default Note;


.App {
  font-family: sans-serif;
  text-align: center;
}
.show{
  display: block;
}
.hide{
  display: none;
}

Instructions

Follow the steps carefully to get the output easily.

  1. Install the Node.js and React on your IDE(preferable Visual Studio Code).
  2. Create React Application using npx create-react-app foldername.
  3. cd foldername.
  4. Open the folder in IDE.
  5. Click on the topic of the code. Copy the code using the "copy" button above and paste it into App.js file. Create CreateArea.js, Note.js, and styles.css files. Copy the codes and paste them into the corresponding files.
  6. Open the terminal from IDE.
  7. npm start to run the file.


You can also refer this url DEMO for getting the output.

I hope you found this useful. I have added the link to dependent libraries, and version information in the following sections.


I found this code snippet by searching for Update Notes in react js in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. The solution is tested on Nodejs 16.14.2 version.
  3. react 18.2.0 version.


Using this solution, we are able to Update Notes in react js with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to Update Notes in react js.

Dependent Libraries

create-react-appby facebook

JavaScript doticonstar image 100082 doticonVersion:v5.0.1doticon
License: Permissive (MIT)

Set up a modern web app by running one command.

Support
    Quality
      Security
        License
          Reuse

            create-react-appby facebook

            JavaScript doticon star image 100082 doticonVersion:v5.0.1doticon License: Permissive (MIT)

            Set up a modern web app by running one command.
            Support
              Quality
                Security
                  License
                    Reuse

                      You can search for any dependent library on kandi like react.

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.

                      See similar Kits and Libraries