【问题标题】:ReactJs: Let submit button auto triggered for once then disable itReactJs:让提交按钮自动触发一次然后禁用它
【发布时间】:2020-06-26 01:20:38
【问题描述】:

我想在页面加载时从 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>
)}

【问题讨论】:

  • 尝试将onClick={this.config()}更改为onClick={this.config} ..删除函数名称后的括号() ..

标签: javascript reactjs api button


【解决方案1】:

只需在 componentDidMount 中调用你的配置函数,它只会在页面加载时调用。 https://reactjs.org/docs/react-component.html#componentdidmount

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多