首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要有关React代码获取请求删除操作的帮助

需要有关React代码获取请求删除操作的帮助
EN

Stack Overflow用户
提问于 2021-02-27 20:25:41
回答 1查看 227关注 0票数 0

我可以发布和获取数据,但无法删除一定是我传递给删除注释函数的id的问题。

这些是我的文件: Note.js

代码语言:javascript
复制
import React, {useState} from 'react';
import DeleteIcon from '@material-ui/icons/Delete';
import EditIcon from '@material-ui/icons/Edit';
import SaveIcon from '@material-ui/icons/Save';

function Note(props) {
  function handleClick() {
    props.onDelete(props.id);
  }

  const[edit, setEdit]=useState(false);

  function editTrue() {
    setEdit(!edit);
  }

  return(
    <div className={!edit ? "note" : "note1"}>
      <h1 contentEditable={edit && "true"} spellCheck="true">{props.title}</h1>
      <hr/>
      <p />
      <p contentEditable={edit && "true"} spellCheck="true">{props.content}</p>
      {!edit && <button onClick={handleClick}><DeleteIcon /></button>}
      {edit ? <button onClick={editTrue}> <SaveIcon /> </button> : <button onClick={editTrue}><EditIcon /></button>}
    </div>
  );
}

export default Note;

App.js

代码语言:javascript
复制
import React, { useState, useEffect } from 'react';
//import { DragDropContext } from 'react-beautiful-dnd';
import Header from './Header';
import Footer from './Footer';
import Note from './Note';
import CreateArea from './CreateArea'

function App() {

  const [notes, setNotes] = useState([]);

  useEffect(() => {
    fetch('https://react-hooks-update-ec587-default-rtdb.firebaseio.com/notes.json')
    .then(response => response.json())
    .then(responseData => {
      const loadedNotes = [];
      for( const key in responseData ) {
        loadedNotes.push({
          id: key,
          title: responseData[key].title,
          content: responseData[key].content
        });
      }
      setNotes(loadedNotes);
    });
  }, []);



  function addNote(newNote) {
    fetch('https://react-hooks-update-ec587-default-rtdb.firebaseio.com/notes.json', {
      method: 'POST',
      body: JSON.stringify(newNote),
      headers: { 'content-Type': 'application/json' }
    }).then(response => {
      return response.json();
    }).then(responseData => {
      setNotes(prevNotes => [
        ...prevNotes,
        { id: responseData.name, ...newNote }
      ]);
    });

  }

  function deleteNote(noteId) {
    fetch(`https://react-hooks-update-ec587-default-rtdb.firebaseio.com/notes/${noteId}.json`,
      {
        method: 'DELETE'
      }
    ).then(response => {
      setNotes(prevNotes => {
        return prevNotes.filter((noteItem, index) => {
          return index !== noteId;
        });
      });
    });

  }

  return(
    <div>
      <Header />
      <CreateArea
        onAdd={addNote}
      />

      {notes.map((noteItem, index) => {
        return (
          <Note
          key={index}
          id={index}
          title={noteItem.title}
          content={noteItem.content}
          onDelete={deleteNote}
        />
      );
      })}

      <Footer />
    </div>
  );
}

export default App;

我可以做哪些更改?当我运行时,我的文件删除操作在本地工作,但它没有从数据库中删除笔记

我可以发布和获取数据,但无法删除一定是我传递给删除注释函数的id的问题。

EN

回答 1

Stack Overflow用户

发布于 2021-02-27 21:23:29

可能在fetch url remove .json中的deleteNote

代码语言:javascript
复制
fetch(`https://react-hooks-update-ec587-default-rtdb.firebaseio.com/notes/${noteId}`)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66398798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档