2

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"

4
  • Do you mean hiding the button and showing just a spinner in place of the button? Commented Mar 19, 2019 at 4:19
  • I mean view spinner on button instead of text Commented Mar 19, 2019 at 4:19
  • Using icons such as FontAwesome popped into my mind. It has several spinning icons too if you can change the text on click. Commented Mar 19, 2019 at 4:23
  • do you mean using spinner instead of Đang đặt lệnh?? Commented Mar 19, 2019 at 5:01

3 Answers 3

2

If you just wanna use spinner instead of text (Đang đặt lệnh), here a way to do.

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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/

If you are using material or semantic, then you can check their docs with "spinners" you'll get there.

Comments

0
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.