简体   繁体   English

Reactjs 单击按钮时添加微调器

[英]Reactjs Add spinner when click button

Here is my code:这是我的代码:

<ButtonOrder
  style={{ background: values.sideNB ? NBColor : NSColor }}
  type="button"
  disabled={isPlacingOrder}
  onClick={handleSubmit}
>
  {isPlacingOrder ? "Đang đặt lệnh" : "Đặt lệnh"}
</ButtonOrder>;

How to change to add spinner instead of text when you click on any button with type "button"单击类型为“button”的任何按钮时,如何更改以添加微调器而不是文本

If you just wanna use spinner instead of text ( Đang đặt lệnh ), here a way to do.如果你只想使用微调器而不是文本( Đang đặt lệnh ),这里有一个方法。

 const ButtonOrder = ({ children, ...rest }) => { return <button {...rest}>{children}</button>; }; const Spinner = () => ( <img src="https://loading.io/spinners/microsoft/index.svg" class="zoom2" height="20"/> ) class App extends React.Component { state = { isPlacingOrder: false }; handleSubmit = () => { this.setState( { isPlacingOrder: true }, () => { setTimeout(() => { this.setState({ isPlacingOrder: false }); }, 2000); } ); } render() { const { handleSubmit, state } = this; const { isPlacingOrder } = state; return ( <ButtonOrder type="button" disabled={isPlacingOrder} onClick={handleSubmit}> {isPlacingOrder ? <Spinner /> : "Đặt lệnh"} </ButtonOrder> ); } } ReactDOM.render(<App />,document.getElementById("app"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

You can check out this implementation for your issues.您可以针对您的问题查看此实现。 If you are using redux or any API call while the loader should be visible (toggle), then that would be simple, but for your current case you can follow this: https://reactjsexample.com/the-easiest-way-to-manage-loaders-errors-inside-a-button/如果您正在使用 redux 或任何 API 调用而加载程序应该是可见的(切换),那么这很简单,但对于您当前的情况,您可以按照以下步骤操作: https : //reactjsexample.com/the-easiest-way-to -manage-loaders-errors-inside-a-button/

If you are using material or semantic, then you can check their docs with "spinners" you'll get there.如果您使用的是材料或语义,那么您可以使用“微调器”检查他们的文档,您将到达那里。

this.state = {
  disabled: false,
  processing: false
}


<TouchableWhatever disabled={this.state.disabled} onPress={this.click.bind(this)}>
  { this.state.processing 
      ? <ActivityIndicator />
      : <Text>Click me</Text>
  }
</TouchableWhatever>


click() {
  this.setState({
    disabled: true, 
    processing: true
  });
  // login, respond
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM