[英]React .setState returns type error
我正在使用React v 16.3.2,正在構建一個應用程序來與Firebase上的數據庫通信訂單,也使用axios 0.18.0。
我期望一旦按下某個按鈕(繼續),它將在加載時在屏幕上創建此微調器效果,因此我有以下代碼:
purchaseContinueHandler = () => {
this.setState = ({loading: true});
const order = {
ingredients: this.state.ingredients,
price: this.state.totalPrice,
customer: {
name: 'Name',
address: {
street: 'teststreet 1',
zipCode: '86753',
country: 'U.S.'
},
email: 'example@example.com'
},
deliveryMethod: 'fastest'
}
axios.post('/orders.json', order)
.then(response => { this.setState( { loading: false } );
})
.catch(error => {
this.setState( { loading: false } ); //**<--This is where i get the error**
});
}
我運行它,然后在應用程序上單擊“繼續”,並收到錯誤消息:
未處理的拒絕(TypeError):_this.setState不是函數
它指向上面列出的行。
跑進這個文檔 ,並試圖實現一個為他工作的解決方案(創建一個constructor()
並綁定this
到this
),但沒有奏效。
我讀到它不再自動添加到函數中,但不確定如何處理該信息,我對React還是很陌生。
有什么建議嗎?
您將在代碼的第一行中為setState
分配一個對象,因此,當您稍后嘗試調用它時,它不再是一個函數...
this.setState = ({loading: true});
應該
this.setState({loading: true})
setState
是一個函數,您將其更改為JavaScript
Object
,我想這是一個小錯誤,您必須編寫:
this.setState({loading: true})
使用它不會將其更改為其他事物。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.