最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to collapse a div with reactjs? - Stack Overflow

programmeradmin2浏览0评论

I am using reactjs and bootstrap v 3.0 but I can't find any collapse class in there so I defined this in my scss. The problem is that the togglebutton does not work or the toggleevent is not triggered on the div:

 toggle(e) {
        console.log('testing e',e)
        if (e.target.class === 'collapse') {
            e.target.className = 'collapse.in'
        } else {
            e.target.className = 'collapse'
        }
    }

This is my button:

<button className="btn btn-block" onClick={() => {
               this.toggle.bind('demo')
               }}>
     Open/close
</button>

How can I change the className from collapse to collapse.in and viceversa? Here is a code sample:Codepen

I am using reactjs and bootstrap v 3.0 but I can't find any collapse class in there so I defined this in my scss. The problem is that the togglebutton does not work or the toggleevent is not triggered on the div:

 toggle(e) {
        console.log('testing e',e)
        if (e.target.class === 'collapse') {
            e.target.className = 'collapse.in'
        } else {
            e.target.className = 'collapse'
        }
    }

This is my button:

<button className="btn btn-block" onClick={() => {
               this.toggle.bind('demo')
               }}>
     Open/close
</button>

How can I change the className from collapse to collapse.in and viceversa? Here is a code sample:Codepen

Share Improve this question edited Aug 28, 2020 at 14:17 Emile Bergeron 17.4k5 gold badges84 silver badges131 bronze badges asked Nov 16, 2016 at 10:10 bier hierbier hier 22.6k44 gold badges102 silver badges174 bronze badges 1
  • shouldn't this be e.target.className = 'collapse in' – Guruprasad J Rao Commented Nov 16, 2016 at 10:35
Add a comment  | 

3 Answers 3

Reset to default 8

Your SCSS and your HTML is fine, the problem is in how you used React.

The open/closed state of your component should be represented with a property in this.state, and the toggle() function should simply toggle that property. The render() function, finally, should be responsible of setting the right CSS class on the <div> it renders.

Here's your Codepen updated with my suggestions.

What I did:

  • I defined an initial state in the component constructor: as you can see, I set the open property to false initially;

  • I rewrote the toggle() method, using this.setState() to toggle the value of the open property;

  • I modified the render() function generating the class name of the <div> depending on the state. If the component state is open, the class name will be "collapse in", else it will be only "collapse".

There is also a library called as "react-bootstrap" which allows the integration of react with bootstrap. They have a very simple example for making the collapsible div, i am sharing the code for this which i have changed according to what i wanted.

You can use this component anywhere inside your code. This will just return a button with collapsible div.

import React, {Component} from 'react'
import {Button, Collapse} from 'react-bootstrap'

class Test extends Component{
    state={ 
        open:false
       }

    render(){
      return(
        <div className= "container">
           <Button className="btn" onClick={!this.state.open}>
               Collapse Div
           </Button>

           <Collapse in={this.state.open}>
               <div>
                  <p>Content when the button is clicked</p>
               </div>
           </Collapse>
        </div>
        );
       }
}

export default Test

There are very nice libraries for this, I wouldn't advice to re-inventing the wheel but use something like react-collapsible You will have lots of flexibility of styling it too.

Install like this : npm i react-collapsible

Then use like this:

import React from 'react';
import Collapsible from 'react-collapsible';

const YourComponent = () => {
  return (
    <Collapsible trigger='Open' triggerWhenOpen='Close'>
      <button className="btn btn-block" onClick={() => {this.toggle.bind('demo') }}>
        This button collapses
      </button>
    </Collapsible>
  );
};
export default YourComponent;

发布评论

评论列表(0)

  1. 暂无评论