反应本地更改按钮状态

时间:2018-08-24 06:54:48

标签: javascript reactjs react-native

我有多个按钮突出显示onPress(ON / OFF)。我想做的是要显示单击了多少个按钮。该图显示选择了4个按钮。我想将状态更改为4并在“ selected:4”中显示 enter image description here

这是我的代码。但是由于某些原因,我无法正确处理。每当我切换按钮时,我都希望状态对应。

state={
    toogle:true,
    count:0
  }

  _onPress() {
    const newState = !this.state.toggle
    this.setState({ toggle: newState })

    const count = this.state.count
    this.setState({count:count})

    this.props.onStateChange && this.props.onStateChange(this.state.count)
    if(this.state.toogle = true ){this.handleIncrement()}
    else if (this.state.toogle = false){this.handleDecrement()}

  }

  handleIncrement = () => {
     this.setState({
         count: this.state.count + 1
     });
    }
    handleDecrement = () => {
       this.setState({
         count: this.state.count - 1
       });
      }

1 个答案:

答案 0 :(得分:0)

您可以创建一个增量方法并在每次点击时调用它

public function access($key)
{
    $query = $this->db->query("SELECT box_role.group_id FROM box_role INNER JOIN box_menu ON box_role.menu_id=box_menu.id WHERE box_menu.key='$key'")->result_array();
    $query = array_column($query, 'group_id');

    return $query;
}