How to collapse a div with reactjs?











up vote
1
down vote

favorite












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






















  • shouldn't this be e.target.className = 'collapse in'
    – Guruprasad Rao
    Nov 16 '16 at 10:35















up vote
1
down vote

favorite












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






















  • shouldn't this be e.target.className = 'collapse in'
    – Guruprasad Rao
    Nov 16 '16 at 10:35













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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













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







twitter-bootstrap reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '16 at 10:10









bier hier

2,33482767




2,33482767












  • shouldn't this be e.target.className = 'collapse in'
    – Guruprasad Rao
    Nov 16 '16 at 10:35


















  • shouldn't this be e.target.className = 'collapse in'
    – Guruprasad Rao
    Nov 16 '16 at 10:35
















shouldn't this be e.target.className = 'collapse in'
– Guruprasad Rao
Nov 16 '16 at 10:35




shouldn't this be e.target.className = 'collapse in'
– Guruprasad Rao
Nov 16 '16 at 10:35












2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










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".







share|improve this answer





















  • That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
    – bier hier
    Nov 16 '16 at 10:46










  • If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
    – lorenzo-s
    Nov 16 '16 at 10:50










  • I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
    – bier hier
    Nov 16 '16 at 10:52










  • Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
    – lorenzo-s
    Nov 16 '16 at 10:54












  • I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
    – bier hier
    Nov 16 '16 at 11:06


















up vote
0
down vote













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





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40629319%2fhow-to-collapse-a-div-with-reactjs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    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".







    share|improve this answer





















    • That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
      – bier hier
      Nov 16 '16 at 10:46










    • If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
      – lorenzo-s
      Nov 16 '16 at 10:50










    • I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
      – bier hier
      Nov 16 '16 at 10:52










    • Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
      – lorenzo-s
      Nov 16 '16 at 10:54












    • I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
      – bier hier
      Nov 16 '16 at 11:06















    up vote
    5
    down vote



    accepted










    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".







    share|improve this answer





















    • That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
      – bier hier
      Nov 16 '16 at 10:46










    • If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
      – lorenzo-s
      Nov 16 '16 at 10:50










    • I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
      – bier hier
      Nov 16 '16 at 10:52










    • Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
      – lorenzo-s
      Nov 16 '16 at 10:54












    • I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
      – bier hier
      Nov 16 '16 at 11:06













    up vote
    5
    down vote



    accepted







    up vote
    5
    down vote



    accepted






    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".







    share|improve this answer












    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".








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '16 at 10:35









    lorenzo-s

    11.2k103668




    11.2k103668












    • That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
      – bier hier
      Nov 16 '16 at 10:46










    • If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
      – lorenzo-s
      Nov 16 '16 at 10:50










    • I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
      – bier hier
      Nov 16 '16 at 10:52










    • Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
      – lorenzo-s
      Nov 16 '16 at 10:54












    • I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
      – bier hier
      Nov 16 '16 at 11:06


















    • That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
      – bier hier
      Nov 16 '16 at 10:46










    • If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
      – lorenzo-s
      Nov 16 '16 at 10:50










    • I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
      – bier hier
      Nov 16 '16 at 10:52










    • Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
      – lorenzo-s
      Nov 16 '16 at 10:54












    • I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
      – bier hier
      Nov 16 '16 at 11:06
















    That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
    – bier hier
    Nov 16 '16 at 10:46




    That is awesome, how/where do I change state to open when I get a change in props which trigger a render? I want to open the panel then.
    – bier hier
    Nov 16 '16 at 10:46












    If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
    – lorenzo-s
    Nov 16 '16 at 10:50




    If you want to open the panel using props instead of state, just use this.props in the render() function instead of this.state. Please note that in that case you don't need to set initial state in constructor and you don't need the toggle() method, because parent components are responsible of changing child components props.
    – lorenzo-s
    Nov 16 '16 at 10:50












    I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
    – bier hier
    Nov 16 '16 at 10:52




    I would like to use props and state. Do I need to read the props in the componentDidMount event? But that would only fire once during initialization?
    – bier hier
    Nov 16 '16 at 10:52












    Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
    – lorenzo-s
    Nov 16 '16 at 10:54






    Props are always available in this.props, everywhere in the component. I suggest you to read a tutorial on React. Props and state usage are the very basic of React, and from your comment it looks like you're confused on what they are and how they work.
    – lorenzo-s
    Nov 16 '16 at 10:54














    I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
    – bier hier
    Nov 16 '16 at 11:06




    I changed your codepen slightly to show you what I mean: codepen.io/gekkerkanniet/pen/oYzGmN?editors=1111. Now it does not close anymore.
    – bier hier
    Nov 16 '16 at 11:06












    up vote
    0
    down vote













    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





    share|improve this answer



























      up vote
      0
      down vote













      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





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 14:28

























        answered Nov 19 at 22:28









        Saurabh Kulkarni

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40629319%2fhow-to-collapse-a-div-with-reactjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga