简体   繁体   English

尝试创建切换功能,根据附加到第三个按钮的onClick事件将2个按钮的状态从禁用设置为启用

[英]Trying to create a toggle function to set the state of 2 buttons from disabled to enabled based on an onClick event attached to a third button

I have a set of 3 buttons where I need to set the initial state for two buttons as disabled and then create an onClick event for a third button that would enable both buttons when clicked. 我有一组3个按钮,我需要将两个按钮的初始状态设置为禁用,然后为第三个按钮创建一个onClick事件,以便在单击时启用这两个按钮。 I'm thinking of setting the disabled attribute in state and then creating the function for onClick that would target the state of both buttons and set it to false . 我正在考虑将disabled属性设置为状态,然后为onClick创建一个函数,该函数将定位两个按钮的状态并将其设置为false My current code is below, any ideas on how to achieve this? 我目前的代码如下,有关如何实现这一点的任何想法?

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Button } from 'antd';
import "antd/dist/antd.css";
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      disabled: undefined
    };
  }

  toggleSwitch(){
    alert("you clicked the switch");
  }

  render() {
    return (
      <div>
        <Button disabled={true}>Modify Docs</Button>
        <Button disabled={true}>Upload Docs</Button>
        <Button onClick={this.toggleSwitch}>Unlock Quote</Button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

You're almost there. 你快到了。

In your render method, you've set disabled={true} which means that it will permanently stay true instead of checking the value of the disabled property in state. 在您的render方法中,您已设置disabled={true} ,这意味着它将永久保持为true,而不是检查状态中disabled属性的值。

The toggle method should simply negate the previous value of disabled . 切换方法应该简单地否定先前的禁用值。

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Button } from 'antd';
import "antd/dist/antd.css";
import './style.css';

class App extends Component {

  state = {
    disabled: true,
  };

  toggleSwitch() {
    // when toggling, we just negate the previous value
    this.setState(previousState => ({
      disabled: !previousState.disabled,
    }))
  }

  render() {
    // Buttons will use the same value from state
    // to check if they should be disabled
    const { disabled } = this.state;

    // instead of setting disabled={true}, reference the disabled
    // property from state
    return (
      <div>
        <Button disabled={disabled}>Modify Docs</Button>
        <Button disabled={disabled}>Upload Docs</Button>

        {/* we set the text of the button depending on the value of disabled */}
        <Button onClick={this.toggleSwitch}>
          {disabled ? 'Unlock Quote' : 'Lock Quote'}
        </Button>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

Also, consider using a toggle component of sorts instead of the third button for better user experience. 此外,请考虑使用各种类型的切换组件而不是第三个按钮,以获得更好的用户体验。

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

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