【问题标题】:Reactjs : How I can change button style when clicked [closed]Reactjs:单击时如何更改按钮样式[关闭]
【发布时间】:2019-05-09 02:24:54
【问题描述】:

我是 ReactJS 的新手,目前我使用ant.design 作为我的界面,在ant.design 我使用Switch Steps 作为向导表单。单击时我想更改下一个按钮的样式。我是这个平台的新手,请指导我

谢谢

【问题讨论】:

标签: javascript reactjs ecmascript-6 antd ant-design-pro


【解决方案1】:

因此,要更改加载状态,您可以将文档中的示例修改为:

import { Steps, Button, message } from 'antd';

const Step = Steps.Step;

const steps = [{
  title: 'First',
  content: 'First-content',
}, {
  title: 'Second',
  content: 'Second-content',
}, {
  title: 'Last',
  content: 'Last-content',
}];

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      current: 0,
      loading: false
    };
  }

  next() {
    const current = this.state.current + 1;
    this.setState({ current, loading: true });
  }

  prev() {
    const current = this.state.current - 1;
    this.setState({ current });
  }

  render() {
    const { current, loading } = this.state;
    return (
      <div>
        <Steps current={current}>
          {steps.map(item => <Step key={item.title} title={item.title} />)}
        </Steps>
        <div className="steps-content">{steps[current].content}</div>
        <div className="steps-action">
          {
            current < steps.length - 1
            && <Button type="primary" loading={loading} onClick={() => this.next()}>Next</Button>
          }
          {
            current === steps.length - 1
            && <Button type="primary" loading={loading} onClick={() => message.success('Processing complete!')}>Done</Button>
          }
          {
            current > 0
            && (
            <Button loading={loading} style={{ marginLeft: 8 }} onClick={() => this.prev()}>
              Previous
            </Button>
            )
          }
        </div>
      </div>
    );
  }
}

这将导致按钮在单击下一步时进入加载状态。但是,您需要一些外部事件被馈送到组件中,以告诉它何时移除加载状态。

这可以通过使用从父组件更改并在componentWillReceiveProps 中检测到的this.prop 来完成,您可以在其中执行this.setState({loading:false}),或者通过使next() 调用一些异步方法来重置加载状态它的回调。

【讨论】:

    猜你喜欢
    • 2020-07-20
    • 2013-07-06
    • 2013-04-17
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2013-04-30
    • 1970-01-01
    相关资源
    最近更新 更多