简体   繁体   English

切换按钮状态并重新使用其他按钮的切换功能

[英]Toggling button state and reusing toggle function for other buttons

I have four button components that I would like to toggle a primary prop component for, and the primary prop is just a true/false input. 我有四个按钮组件,我想为其切换主要道具组件,而主要道具只是一个true / false输入。 When true it changes the button color to purple, when false it is grey. 如果为true,则将按钮颜色更改为紫色;如果为false,则将其更改为灰色。 Currently, the only way I would know how to do this is to create a new toggleProfitView function that explicitly states setState({revenueb: !this.state.revenueb}) , and another that does setState({profitb: !this.state.profitb}) for example, but I have four different button states and I'd like to be able to just reuse a function that knows which button to toggle. 当前,我唯一会知道如何执行此操作的方法是创建一个新的toggleProfitView函数,该函数明确声明setState({revenueb: !this.state.revenueb}) ,另一个声明setState({profitb: !this.state.profitb})的例子,但我有四个不同的按钮状态,我希望能够只重用知道切换的按钮的功能。 I'm trying to be DRY with my code. 我正在尝试将我的代码干燥。 In pseudocode it would be something like, 用伪代码会是这样,

this.button.primary.state = !this.button.primary.state

Any ideas? 有任何想法吗? Thanks. 谢谢。

toggleProfitView(key, event) {
    console.log(event)
    var plot_number = this.state.plot_number
     this.setState({variant_plot_data: this.props.final_plot[plot_number]}, () => {
      this.updatePlots(key);
    });
  }

function PlotIfDataExists(props) {
  const dataExists = props.dataExists;
  if(dataExists) {
    return (<div>
              <ProductGraphData variant_plot_data = {variant_plot_data} />
              <Stack spacing="none" distribution="leading">
                <Select
                  options={props.variants}
                  placeholder="Select"
                  onChange={props.handleVariantChange}
                />
                <Button onClick={props.toggleVariantPlotData}>Next Plot</Button>
                <Button primary={props.revenueb} onClick={(event) => props.toggleView('revenue', event)}>Revenue Plot</Button>
                <Button primary={props.profitb} onClick={(event) => props.toggleView('profit', event)}>Profit Plot</Button>
                <Button primary={props.profitvb} onClick={(event) => props.toggleView('profit_per_view', event)}>Profit/View Plot</Button>
                <Button primary={props.revenuevb} onClick={(event) => props.toggleView('rev_per_view', event)}>Rev/View Plot</Button>
                <Button onClick={props.showAllPlots}>Show All</Button>
              </Stack>
              <LastPriceTestContainer analytics_data = {variant_plot_data} />
            </div>
    );
  }
  return null;
}

I solved this problem using a hash to maintain the states. 我使用哈希来维护状态来解决此问题。

 toggleView(key, event) {
    const button_states_hash = Object.assign({}, this.state.button_states);
    button_states_hash[key] = !button_states_hash[key]
    var plot_number = this.state.plot_number
     this.setState({
       variant_plot_data: this.props.final_plot[plot_number],
       button_states: button_states_hash
     }, () => {
      this.updatePlots(key);
    });
  }

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

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