2017-10-18 5 views
0

私はフロントエンドにReact.jsを、バックエンドにExpress.jsを持っています。現時点では、React with Deleteリクエストから投稿を削除しようとしていますが、削除後に新しいリストを表示する前にURLに移動してEnterキーを押す必要があります。リフレッシュする必要なく、ページを再レンダリングしたいと思います。削除要求を送信した後の再レンダリングが反応しない

class Rentals extends Component { 

    constructor(props) { 
     super(props); 
     this.state = { 
      completelist: [], 
      apiLoaded: false, 
     } 
     this.conditionalRentList = this.conditionalRentList.bind(this); 
     this.handleDeleteButton = this.handleDeleteButton.bind(this); 
     this.fetchAllRentals = this.fetchAllRentals.bind(this); 
    } 

    componentDidMount() { 
     this.fetchAllRentals(); 
    } 

    fetchAllRentals() { 
     fetch('api/listofrentals') 
      .then((res) => { 
       return res.json() 
      }) 
      .then((fullrental) => { 
       this.setState((prevState) => { 
        return { 
         completelist: fullrental.rentalsData, 
         apiLoaded: true 
        } 
       }) 
      }) 
    } 

    conditionalRentList() { 
     if (this.state.apiLoaded === true) { 
      return (
       <RentalsComp 
        completelist={this.state.completelist} 
        handleDeleteButton={this.handleDeleteButton} 
       /> 
      ); 
     } else { 
      return <p> Loading </p> 
     } 
    } 

    handleDeleteButton(idToBeDeleted) { 
     fetch(`api/listofrentals/${idToBeDeleted}`, { 
      method: 'DELETE' 
     }).then((res) => { 
      if (res.json === 200) { 
       this.fetchAllRentals(); 
      } 
     }) 
    } 

} 

機能handleDeleteButtonは、すべてのリストを取得し、それがfetchAllRentals関数を呼び出しますが、その理由は、いくつかのために働いていないので、リフレッシュすることなく、それをレンダリングする必要があります。

+1

あなたの 'this.setState'が実際に働いているのかどうかは、' this'の値があなたの期待通りではないのではないかと思います。これを関数の先頭にある変数に設定してみてください。例えば ​​'fetchAllRentals(){var that = this'なら' that.setState'を使うことができます... –

+2

DELETE APIを呼び出すときにあなたの 'resp.json'が何であるかチェックできますか? – palsrealm

+0

こんにちは、何が間違っているのかを見つけ出し、 'resp.status === 200'を実行して、それが通過してからフェッチするかどうかを確認したかったのです。私は何らかの理由でresp.jsonをやっていました。ありがとう – craftdeer

答えて

0

resp.status === 200としていましたが、代わりにresp.json === 200を行っていました。

関連する問題