亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

更新組件以反映數據的變化?

更新組件以反映數據的變化?

揚帆大魚 2021-12-02 14:51:40
嘗試在“創建”組件中創建一個新注釋發送了一個 POST 請求并將其添加到數據庫中?,F在我需要將更改反映在“App”組件的狀態中。解決方案:在將 Note (Create Comp) 提交到數據庫時,將對象的副本發送到 App.js,在那里我們更新 state 中的 note 屬性,以便它反映數據庫中的更改。問題:出現一些錯誤,代碼中標記了問題行。class App extends Component{  state = {    notes: []  }  componentDidMount(){    fetch('http://localhost:3001/notes')      .then(resp => resp.json())      .then(data => this.setState({notes: data}))  }  onSubmitUpdateState(newNote){    const oldState = [...this.state.notes]    // Problem 2)    this.setState({notes: [...oldState, newNote]});  }  render(){    return(      <div className="App">        <Notes notes={this.state.notes}/>        <Create updateState={this.onSubmitUpdateState}/>      </div>    )  }}export default App;import React, { Component } from 'react';import classes from './Create.module.css';class Create extends Component{  state= {    title: "",    body: ""  }    onChange = (e)=>{    this.setState({[e.target.name]: e.target.value})  }  handleSubmit = (e)=>{    e.preventDefault();    const post = {      title: this.state.title,      body: this.state.body    }    // Problem 1    fetch('http://localhost:3001/notes', {      method: 'POST',      headers: {        'Content-Type': 'application/json'      },      body: JSON.stringify(post)    })      .then(res => res.json())      .then(data => this.props.updateState(data))    this.setState({      title: "",      body: ""    })  }
查看完整描述

3 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

如果您在任何方法中使用this.state,請使用箭頭函數或綁定您的操作。


箭頭函數:


  onSubmitUpdateState = (newNote) => {

    const oldState = [...this.state.notes]

    // Problem 2)

    this.setState({notes: [...oldState, newNote]});

  }

綁定方法: 創建構造函數并在構造函數中添加這一行


 this.onSubmitUpdateState = this.onSubmitUpdateState.bind(this)

沒有構造函數


<Create updateState={this.onSubmitUpdateState.bind(this)}/> 


查看完整回答
反對 回復 2021-12-02
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

在 Javascript 中,類方法在默認情況下不受限制,這個 SO 答案討論了為什么會這樣?


如果您不使用箭頭函數,請確保bind()您的事件處理程序能夠訪問this.


<Create

  updateState={this.onSubmitUpdateState.bind(this)} // pass a binded handler

/>


查看完整回答
反對 回復 2021-12-02
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

嘗試這樣做


class App extends Component{

  state = {

    notes: []

  }


  componentDidMount(){

    fetch('http://localhost:3001/notes')

      .then(resp => resp.json())

      .then(data => this.setState({notes: data}))

  }




  onSubmitUpdateState = (newNote) => {   //change to arrow function//

    const oldState = [...this.state.notes]

    // Problem 2)

    this.setState({notes: [...oldState, newNote]});

  }

  render(){

    return(

      <div className="App">

        <Notes notes={this.state.notes}/>

        <Create updateState={this.onSubmitUpdateState}/>

      </div>

    )

  }

}


export default App;

箭頭語法this為您處理綁定。或者您可以bind在構造函數中使用該函數。`


基本上箭頭函數this在調用時會保留上下文


查看完整回答
反對 回復 2021-12-02
  • 3 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號