簡體   English   中英

ReactJs:讓提交按鈕自動觸發一次然后禁用它

[英]ReactJs: Let submit button auto triggered for once then disable it

我想在頁面加載時從 API 獲取 [myData] 一次。 我的計划是使用隱藏按鈕,讓它自動觸發。 但我的問題是按鈕不斷觸發。

如何更改我的代碼以使其自動觸發一次。

我的代碼:

state = {myData: ''}
config = () => {
   axios.get('http://mySite')
   .then(res => this.setState({myData: res.data}))
}
render() {
return (
  <div>
    <button style={{visibility:'hidden'}} onClick={this.config()}></button>
  </div>
)}

只需在 componentDidMount 中調用您的配置 function,它只會在頁面加載時調用。 https://reactjs.org/docs/react-component.html#componentdidmount

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM