简体   繁体   English

更改文本时如何更新 Note 元素?

[英]How can I get my Note element to update when I change its text?

sorry for the long post, but I'm really stuck in this part of my project.对不起,很长的帖子,但我真的被困在我项目的这一部分。 I'm using React to build a notes app, and I can't get my note to update when I alter the text inside of my modal popup...the commented out parts are where I'm having trouble (I wrote what I thought of doing, but don't know how. I've been using React for less than a week).我正在使用 React 构建一个笔记应用程序,当我更改模式弹出窗口中的文本时,我无法更新我的笔记......注释掉的部分是我遇到问题的地方(我写了我想做,但不知道怎么做。我使用 React 不到一周)。 Thank you in advance!先感谢您!

App.js应用程序.js

import React, { useState } from "react";
import { nanoid } from "nanoid";
import NotesList from "./Components/NotesList";
import AddNote from "./Components/AddNote";

const App = () => {
  const [notes, setNotes] = useState([
    /*empty array of notes*/
  ]);

  const addNote = (title, text) => {
    const date = new Date();
    const newNote = {
      id: nanoid(),
      title: title,
      text: text,
      date: date.toDateString(),
    };
    const newNotes = [...notes, newNote];
    setNotes(newNotes);
  };

  const deleteNote = (id) => {
    const newNotes = notes.filter((note) => note.id !== id);
    setNotes(newNotes);
  };

  const editNote = (text, title, id) => {
    const editDate = new Date();
    const editedNote = {
      id: nanoid(),
      title: title,
      text: text,
      date: editDate.toDateString(),
    };
    
    const editedNotes = notes.map((note) => {
      if (note.id == id) {
        return editedNote;
      }
      return note;
    });
    setNotes(editedNotes);
  };

  return (
    <div className="container">
      <AddNote handleAddNote={addNote} />
      <NotesList
        notes={notes}
        handleAddNote={addNote}
        handleDeleteNote={deleteNote}
        editNote={editNote}
      />
    </div>
  );
};

export default App;

NotesList.js NotesList.js

import Note from "./Note";
import React, { useState } from "react";
import MyModal from "./Modal";

const NotesList = ({ notes, handleDeleteNote, updateNote, modifyNote }) => {
  const [open, setOpen] = useState(false);
  const [note, setNote] = useState({});

  const handleOpen = (id) => {
    setNote(notes.filter((note) => note.id === id)[0]);
    setOpen(true);
  };

  const handleClose = () => {
    setNote({});
    setOpen(false);
  };
  const clickedTodo = useState({});
  return (
    <div className="notes-list">
      <div className="blank-note"></div>
      <div className="existing-notes">
        <MyModal
          note={note}
          clickedTodo={clickedTodo}
          handleClose={handleClose}
          open={open}
          updateNote={updateNote}
        />
        {notes.map((note) => (
          <Note
            id={note.id}
            title={note.title}
            text={note.text}
            date={note.date}
            handleOpen={handleOpen}
            handleDeleteNote={handleDeleteNote}
          />
        ))}
      </div>
    </div>
  );
};

export default NotesList;

AddNote.js AddNote.js

import { useState } from "react";
import React from "react";

const AddNote = ({ handleAddNote, isUpdate, note, updateNote }) => {
  const [noteTitle, setNoteTitle] = useState("");
  const [noteText, setNoteText] = useState("");
  const characterLimit = 300;

  const handleChange = (event) => {
    if (characterLimit - event.target.value.length >= 0) {
      setNoteText(event.target.value);
    }
  };

  const handleChange2 = (event) => {
    if (characterLimit - event.target.value.length >= 0) {
      setNoteTitle(event.target.value);
    }
  };
 
  const handleSaveClick = () => {
    // if (!isUpdate) then do this
    // if (!isUpdate) {
      (noteText.trim().length > 0)
        handleAddNote(noteTitle, noteText);
        setNoteText("");
        setNoteTitle("");
    // } else updateNote(note.id)
    // else we use all of the info we have gotten
    // we call update note with note.id
  };

  return (
    <div className="note new">
      <h2>Add note</h2>
      <textarea className="new-text-title"
        placeholder={!isUpdate?"Add a title...":note.title}
        value={noteTitle}
        onChange={handleChange2}
      ></textarea>
      <textarea className="new-text-body"
        cols="8"
        rows="10"
        placeholder={!isUpdate?"Type your note here...":note.text}
        value={noteText}
        onChange={handleChange}
      ></textarea>
      <div className="note-footer">
        <small>Characters remaining: {characterLimit - noteText.length}</small>
        <button className="save" onClick={handleSaveClick}>
          <strong>Save</strong>
        </button>
      </div>
    </div>
  );
};

export default AddNote;

Note.js Note.js

import { MdDeleteForever } from "react-icons/md";

const Note = (props) => {
  const { id, title, text, date, handleOpen, handleDeleteNote } = props;
  const handleOpenModal = (id) => {
    handleOpen(id);
  };
  return (
    <div className="note">
      <div className="note-upper" onClick={() => handleOpenModal(id)}>
        <p className="title">
          <textarea className="text-title">{title}</textarea>
        </p>
        <textarea className="text-body">{text}</textarea>
      </div>
      <div className="note-footer">
        <div className="footer-left" onClick={() => handleOpenModal(id)}>
          <small>{date}</small>
        </div>
        <div className="footer-right">
          <MdDeleteForever
            onClick={() => {
              if (window.confirm("Delete this note?")) {
                handleDeleteNote(id);
              }
            }}
            className="delete-icon"
            size="1.3em"
          />
        </div>
      </div>
    </div>
  );
};

export default Note;

Modal.js模态.js

import React, { useState } from "react";
import { Modal, Box } from "@mui/material";
import Note from "./Note";

export default function MyModal(props) {
  const { open, handleClose, note, updateNote } = props;
  return (
    <div onClose={console.log("closing")}>
      <Modal open={open} onClose={handleClose}>
        <Box
          sx={{
            position: "relative",
            top: "50%",
            left: "32%",
            outline: "none",
          }}
        >
          <Note
            updateNote={updateNote}
            note={note}
            id={note.id}
            title={note.title}
            text={note.text}
            date={note.date}
          />
        </Box>
      </Modal>
    </div>
  );
}

Your Modal.js requires updateNote prop, but in App.js you don't supply it to NotesList.js, thus the parent prop of MyModal.js has no such prop:您的 Modal.js 需要 updateNote 道具,但在 App.js 中您不将其提供给 NotesList.js,因此 MyModal.js 的父道具没有这样的道具:

<NotesList
     notes={notes}
     handleAddNote={addNote}
     handleDeleteNote={deleteNote}
     editNote={editNote}
/>

And later in NotesList.js:后来在 NotesList.js 中:

<MyModal
      note={note}
      clickedTodo={clickedTodo}
      handleClose={handleClose}
      open={open}
      updateNote={updateNote} // this is missing because you are not
                              // passing this prop from the App component
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不影响其子元素的情况下更改元素的文本? - How can I change the text of an element without affecting its child element? 如何在不更改其当前元素的情况下更改其文本? - How can I change an element's text without changing its current element? 如何让我的 Redux useSelector 更新商店变更? - How can I get my Redux useSelector to update on store change? 当粘性元素粘在其父元素的底部时,如何更改粘性元素的样式? - How can I change the style of a sticky element when the sticky element is stuck to the bottom of its parent? 如何在不更改元素的子元素的情况下更改元素的文本? - How can I change an element's text without changing its child elements? 如何更改<span>元素</span>的文本<span>?</span> - How can I change the text of a <span> element? 如何使用jQuery更改元素中的文本? - How can I change the text in element with jQuery? 如何向自定义元素添加属性,并能够对其值更改触发操作 - How can I add a property to my custom element and be able to fire an action on its value change 当用逗号分隔输入时,如何将html中的文本框内容作为单独的单词? - How can I get content of my text-box in html as separate words when its entered separated by a comma? 如果我处于其EventListener的函数中,如何获取Element - How can I get the Element if I am in the function of its EventListener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM