react expand and collapse just one panel

前端 未结 2 631
难免孤独
难免孤独 2021-02-14 18:34

Need help with react... Trying to implement a collapsible list of cards with weather information. Already implemented the behavior of expand and collapse, but w

2条回答
  •  醉话见心
    2021-02-14 19:10

    I would have an object to represent the state, a field for each panel.

    Like this:

    constructor(props) {
        ...
        this.state = {
          requestFailed: false,
          shown: {}
        }
        ...
    }
    
    ...
    
    toggle(panelNumber) {
       this.setState({
            shown: {
                ...this.state.shown,
                [panelNumber]: !this.state.shown[panelNumber]
            }
        });
    }
    
    ...
    

    The toogle function is used like this, for instance, Day 1:

    this.toggle(1)} className="dayWeekItem"> ...

    And to show in html, for instance, Day 1:

          

提交回复
热议问题