超碰中文网,91久久精品日日躁夜夜躁欧美,一区二区三区精品道,一区二区国产在线观看,亚洲精品中文字幕乱码一区二区,天天看片高清影视在线观看,久久99精品久久久久蜜桃,久久成人影视

VPS參考測評推薦
專注分享VPS主機優惠信息
衡天云優惠活動
熱網互聯2026年促銷活動
hostkvm優惠促銷活動
zji優惠促銷活動
QQGNET優惠促銷活動
QQGNET優惠促銷活動

基于React在博客應用程序中更新和刪除帖子:第4部分

主機參考:VPS測評參考推薦/專注分享VPS服務器優惠信息!若您是商家可以在本站進行投稿,查看詳情!此外我們還提供軟文收錄、PayPal代付、廣告贊助等服務,查看詳情!
我們發布的部分優惠活動文章可能存在時效性,購買時建議在本站搜索商家名稱可查看相關文章充分了解該商家!若非中文頁面可使用Edge瀏覽器同步翻譯!PayPal代付/收錄合作

在本系列教程的前一部分中,您學習了如何添加和顯示帖子。在react中創建博客應用程序系列教程的這一部分中,您將實現更新和刪除博客帖子的功能。

入門讓我們開始克隆本系列最后一部分的源代碼。

https://github.com/royagasthyan/ReactBlogApp-AddPost克隆目錄后,導航到項目目錄并安裝所需的依賴項。

CD ReactBlogApp-AddPost NPM安裝啟動Node.js服務器,應用程序將在http://localhost:7777/index . html #/上運行。

創建更新和刪除視圖讓我們修改博客文章列表,并使用更新和刪除圖標以表格形式顯示數據。在ShowPost組件的render方法中,用表格替換現有的div,如代碼所示:

& lttable className = & quot表格表格-條紋& quot& gt& ltthead & gt& lttr & gt& ltth & gt# & lt/th & gt;& ltth & gtTitle & lt/th & gt;& ltth & gt主題& lt/th & gt;& ltth & gt& lt/th & gt;& ltth & gt& lt/th & gt;& lt/tr & gt;& lt/thead & gt;& lttbody & gt{ this . state . posts . map(function(post,index){ return & lt;tr key = { index } & gt& lttd & gt{ index+1 } & lt;/TD & gt;& lttd & gt{ post.title } & lt/TD & gt;& lttd & gt{ post.subject } & lt/TD & gt;& lttd & gt& ltspan className = & quot雕花鉛筆& quot& gt& lt/span&gt。& lt/TD & gt;& lttd & gt& ltspan className = & quot雕花圖標雕花圖標-刪除& quot& gt& lt/span&gt。& lt/TD & gt;& lt/tr & gt;}.bind(this))} } & lt;/tbody & gt;& lt/table & gt;如上面的代碼所示,您已經修改了現有代碼,以表格形式顯示帖子。您已經映射了posts變量以迭代posts集合并動態創建所需的tr和td。

保存上述更改并重新啟動服務器。將瀏覽器指向http://localhost:7777/home#/,您應該能夠以表格形式查看博客文章列表。

在基于 React 的博客應用程序中更新和刪除帖子:第 4 部分

要實現更新發布功能,需要將click事件附加到編輯圖標上。如下圖所示修改編輯圖標范圍:

& ltspan onClick = { this . update Post . bind(this,post。_ id)class name = & quot;雕花鉛筆& quot& gt& lt/span&gt。如上面的代碼所示,您已經將帖子ID作為參數傳遞給updatePost方法。

在ShowPost組件中創建一個updatePost方法。

update post(id){ hash history . push(‘/add post/‘+id);}如上面的代碼所示,您已經觸發了到添加文章頁面的重定向,該頁面具有已編輯項目的ID。在Add Post頁面上,您將使用傳遞的ID獲取博客文章的詳細信息并填寫詳細信息。

修改路由器,在添加帖子頁面中包含可選的id參數。

& lt路由組件={AddPost}路徑= & quot/addPost(/:id)& quot;& gt& lt/Route & gt;在AddPost組件中,創建一個名為getPostWithId的方法,使用該Id獲取博客文章的詳細信息。在getPostWithId方法中,對app.js中的getPostWithId API進行AJAX調用

getPostWithId(){ var id = this . props . params . id;var self = thisaxios . post(“/getPostWithId“,{ id: id })。then(function(response){ if(response){ self . setstate({ title:response . data . title });self . setstate({ subject:response . data . subject });} }) .catch(function(error){ console . log(‘error is‘,error);});}您已經通過從getPostWithId API方法收到的響應更新了狀態變量title和subject。

修改標題和主題文本框以顯示狀態變量的值。

& ltdiv className = & quot表單組& quot& gt& lt輸入值={this.state.title}類型= & quot文本& quotonChange = { this . handletitlchange } class name = & quot;表單控件& quotid = & quot標題& quotname = & quot標題& quotplaceholder = & quot標題& quot必需/& gt;& lt/div & gt;& ltdiv className = & quot表單組& quot& gt& lttextarea value = { this . state . subject } class name = & quot;表單控件& quotonChange = { this . handlesubjectchange } type = & quot;textarea & quotid = & quot主題& quotplaceholder = & quot主題& quotmaxlength = & quot140 & quotrows = & quot7 & quot& gt& lt/textarea & gt;& lt/div & gt;現在,讓我們在app.js中創建getPostWithId API來對MongoDB數據庫進行數據庫調用,以獲取具有特定Id的帖子詳細信息。這是getPostWithId API方法:

app . post(‘/getPostWithId‘,function(req,RES){ var id = req . body . id;post . getPostWithId(ID,function(result){ RES . send(result)})在post.js文件中,創建一個getpostwithid方法來查詢數據庫以獲取詳細信息。其外觀如下:

getPostWithId:function(id,callback){ mongo client . connect(URL,function(err,db){ db . collection(‘post‘)。find one({ _ id:new MongoDB。ObjectID(id)},function(err,result){ assert . equal(err,null);if(err = = null){ callback(result)} else { callback(false)} });})}如上面的代碼所示,您使用findOne API獲取了具有特定ID的博客文章的詳細信息。

保存以上更改并嘗試運行程序。單擊主頁上的編輯圖標,這將重定向到添加文章頁面并填寫標題和主題。

在基于 React 的博客應用程序中更新和刪除帖子:第 4 部分

現在,要更新博客文章的詳細信息,您需要檢查id是否在app.js的addPost API方法中。如果是新文章,id將未定義。

修改AddPost組件中的AddPost方法以包含id狀態變量。

axios . post(‘/add post‘),{title: this.state.title,subject: this.state.subject,id: this.state.id})在addPost API方法中,需要檢查id參數是否未定義。如果未定義,則意味著這是一篇新帖子,否則需要調用update方法。addPost API方法如下:

app . post(‘/add post‘,function(req,RES){ var title = req . body . title;var submit = req . body . subject;var id = req . body . id;if(id = =‘‘‘| | id = = undefined)post . add post(title,subject,function(result){ RES . send(result);});} else { post . update post(id,標題,主題,函數(結果){ res.send(結果);});}})在post.js文件中,創建一個名為updatePost的方法來更新博客文章的詳細信息。您將使用updateOne API來更新具有特定id的博客文章的詳細信息。下面是updatePost方法的樣子:

update post:function(id,title,subject,callback){ mongo client . connect(URL,function(err,db){ db . collection(‘post‘)。update one({ & quot;_ id & quot:新mongodb。ObjectID(id)},{ $ set:{ & quot;標題& quot:標題主題& quot:subject } },function(err,result){ assert . equal(err,null);if(err = = null){ callback(true)} else { callback(false)} });});}保存以上更改并重新啟動服務器。登錄應用程序并單擊編輯圖標。修改現有值并單擊按鈕更新詳細信息。

要刪除文章,您需要在刪除圖標上附加一個點擊事件。如圖所示修改和刪除圖標范圍:

& ltspan onClick = { this . delete Post . bind(this,post。_ id)class name = & quot;雕花圖標雕花圖標-刪除& quot& gt& lt/span&gt。如上面的代碼所示,您已經將帖子ID作為參數傳遞給deletePost方法。

在ShowPost組件中創建一個名為deletePost的方法。

delete post(id){ }在ShowPost組件的構造函數中綁定此方法。

this . delete post = this . delete post . bind(this);要在映射函數回調中使用它,您需要將它綁定到映射函數。修改地圖函數回調如下圖所示:

{ this . state . posts . map(function(post,index){ return { index+1 } { post . title } { post . subject } & lt;span onClick = { this . update Post . bind(this,post。_ id)class name = & quot;雕花鉛筆& quot& gt& lt/span&gt。& ltspan onClick = { this . delete Post . bind(this,post。_ id)class name = & quot;雕花圖標雕花圖標-刪除& quot& gt& lt/span&gt。}.bind(this)}在deletePost方法中,在調用delete API之前添加確認提示。

delete post(id){ if(confirm(‘您確定嗎?’)){ //刪除Post API調用將在此處!!}}現在讓我們將deletePost API添加到app.js文件中。API將從AJAX調用中讀取文章ID并從MongoDB中刪除條目。下面是deletePost API的樣子:

app . post(“/delete post”,function(req,RES){ var id = req . body . id;post . deletePost(ID,function(result){ RES . send(result)})如上面的代碼所示,您將調用post.js文件中的delete post方法并返回結果。讓我們在post.js文件中創建deletePost方法。

delete post:function(id,callback){ mongo client . connect(URL,function(err,db){ db . collection(‘post‘)。delete one({ _ id:new MongoDB。ObjectID(id)},function(err,result){ assert . equal(err,null);console . log(& quot;刪除了帖子。");if(err = = null){ callback(true)} else { callback(false)} });})}如上面的代碼所示,post.js文件中的deletePost方法將使用MongoClient連接到MongoDB中的博客數據庫。使用AJAX調用傳遞的Id,它將從數據庫中刪除帖子。

更新home.jsx文件中deletePost方法的代碼,將AJAX調用app.js文件包含到deletePost API中。

delete post(id){ if(confirm(‘您確定嗎?’)){ var self = thisaxios . post(“/delete post“,{ id: id })。然后(function(response){ })。catch(function(error){ });}}刪除博客文章后,您需要刷新博客文章列表以反映這一點。因此,創建一個名為getPost的新方法,并將componentDidMount代碼移到該函數中。這是getPost方法:

get post(){ var self = this;axios . post(‘/get post‘,{ })。then(function(response){ console . log(‘RES is‘,response);self . setstate({ posts:response . data })})。catch(function(error){ console . log(‘error is‘,error);});}修改componentDidMount代碼,如圖:

componentidmount(){ this . get post();document . getelementbyid(‘home hyperlink‘)。className = & quot活動& quot;document . getelementbyid(“add hyperlink“)。className = & quot";}調用getPost方法在deletePost AJAX調用的成功回調中更新博客文章列表。

delete post(id){ if(confirm(‘您確定嗎?’)){ var self = thisaxios . post(“/delete post“,{ id: id })。then(function(response){ self . get post();}) .catch(function(Error){ console . log(‘Error is‘,Error);});}}保存以上更改并重新啟動服務器。嘗試添加新的博客文章,然后從網格列表中單擊刪除。系統將提示您刪除確認消息。單擊“確定”按鈕后,該條目將被刪除,博客文章列表將被更新。

在基于 React 的博客應用程序中更新和刪除帖子:第 4 部分

總結在本教程中,您學習了如何在React博客應用程序中刪除和更新博客文章。在本系列教程的下一部分中,您將學習如何為登錄用戶實現個人資料頁面。

請在下面的評論中告訴我們你的想法和建議。本教程的源代碼可以在GitHub上找到。

這幾篇文章你可能也喜歡:

  • 暫無相關推薦文章

本文由主機參考刊發,轉載請注明:基于React在博客應用程序中更新和刪除帖子:第4部分 http://www.k2270.com/109889.html

【騰訊云】領8888元采購禮包,搶爆款云服務器 每月 9元起,個人開發者加享折上折!
打賞
轉載請注明原文鏈接:主機參考 » 基于React在博客應用程序中更新和刪除帖子:第4部分
主機參考僅做資料收集,不對商家任何信息及交易做信用擔保,購買前請注意風險,有交易糾紛請自行解決!請查閱:特別聲明

評論 搶沙發

評論前必須登錄!