Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating show and hide sections with buttons in reactjs

I have three buttons that when clicking show and individual div but this is done in reactjs

import React, { Component } from 'react';

export class ModeExtended extends Component {

constructor() {
    super();

    this.busButton = this.busButton.bind(this);
    this.trainButton = this.trainButton.bind(this);
    this.tramButton = this.tramButton.bind(this);

    this.state = {
        isHidden: false,
    }
}


busButton(){
    console.log('Bus Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHidden: !prevState.isHidden
        };
    });
}

trainButton(){
    console.log('Train Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHidden: !prevState.isHidden
        };
    });
}

    tramButton(){
    console.log('Tram Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHidden: !prevState.isHidden
        };
    });
}


render() {
    return (
        <div>   
            <h5>Mode Extended</h5>

            <button onClick={this.busButton}>Bus</button>
            <button onClick={this.trainButton}>Train</button>
            <button onClick={this.tramButton}>Tram</button>





            {this.state.isHidden && (
                <div>
                    <h6>You can show Bus Data Now....</h6>
                </div>
            )}

            {this.state.isHidden && (
                <div>
                    <h6>You can show Train Data Now....</h6>
                </div>
            )}

            {this.state.isHidden && (
                <div>
                    <h6>You can show Tram Data Now....</h6>
                </div>
            )}


        </div>
    )
}
}

export default ModeExtended

When I click any of the buttons it shows all bus, tram and train data - how do I get them to just show one thing at a time and making sure that the other states are closed. I am really missing something here and need a pointer or two or three…

How can I add an ID to make each button open separate from each other and when one is clicked how can I close the rest of the divs - or open state, I am so lost here. Please help me out.

Cheers as always!

Here is a REPL of my code:

like image 446
Tired_Man Avatar asked Dec 10 '19 08:12

Tired_Man


People also ask

How to hide or show a component in ReactJS?

Step 1: Create a React application using the following command: Hide or Show Component in ReactJS As we discussed above we have to create few components and render them into a single parent file to execute the conditions so that we can apply the functionality of showing or hiding to some particular components.

How to show or hide components directly from the button click?

From the button click event, we will get a string that identifies which button is clicked. Based on the given string, the appropriate state value will be updated. This is how, based on the state value and the logical and operator, we can show or hide the components directly. Other ways are also possible.

How to show/hide components based on condition in state objects?

In state objects, we have three different Boolean variables with false as the default value, and these variables will be used to show or hide the specific component. We will use logical and operator, (&&), in order to show components based on the condition along with the button click event, which is explained in the following example.

How to show components based on condition along with button click event?

In state objects, we have three different Boolean variables with false as the default value, and these variables will be used to show or hide the specific component. We will use logical and operator, (&&), in order to show components based on the condition along with the button click event, which is explained in the following example. Index.js


2 Answers

You need to have 3 different isHidden properties to control your divs. You can do it like this:

this.state = {
    isHiddenBus: false,
    isHiddenTrain: false,
    isHiddenTram: false,
    }

and then in your render like this:

{this.state.isHiddenBus && (
                <div>
                    <h6>You can show Bus Data Now....</h6>
                </div>
            )}

            {this.state.isHiddenTrain && (
                <div>
                    <h6>You can show Train Data Now....</h6>
                </div>
            )}

            {this.state.isHiddenTram && (
                <div>
                    <h6>You can show Tram Data Now....</h6>
                </div>
            )}

also your buttons have to change to state accordingly to this.

busButton(){
    console.log('Bus Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHiddenBus: !prevState.isHiddenBus
            isHiddenTram: false
            isHiddenTrain: false
        };
    });
}

trainButton(){
    console.log('Train Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHiddenTrain: !prevState.isHiddenTrain
            isHiddenBus: false
            isHiddenTram: false

        };
    });
}

    tramButton(){
    console.log('Tram Button Was Pressed');
    this.setState((prevState) => {
        return{
            isHiddenTram: !prevState.isHiddenTram
            isHiddenTrain: false
            isHiddenBus: false
        };
    });
}
like image 172
octobus Avatar answered Sep 21 '22 10:09

octobus


you can do somthing like this:

            import React, { Component } from 'react';

            export class ModeExtended extends Component {

            constructor() {
                super();

                this.state = {
                    curDivIndex:0,//currently visible div index
            //        isHidden: false,
                }
            }


            renderDiv=()=>{
                switch(this.state.curDivIndex){
                    case 1:return  <div>        <h6>You can show Bus Data Now....</h6>    </div>
                    case 2:return  <div>       <h6>You can show Train Data Now....</h6>   </div>
                    case 3:return  <div>       <h6>You can show Tram Data Now....</h6>   </div>
                }
                return null
            }
            setVisibleDiv=(index)=>{
                this.setState({curDivIndex:index})
            }

            render() {
                return (
                    <div>   
                        <h5>Mode Extended</h5>
                        <button onClick={()=>{this.setVisibleDiv(1)} }>Bus</button>
                        <button onClick={()=>{this.setVisibleDiv(2)}}>Train</button>
                        <button onClick={()=>{this.setVisibleDiv(3)}}>Tram</button>
                        {this.renderDiv()}
                    </div>
                )
            }
            }

            export default ModeExtended

EDIT

you want to have three different buttons, on click of each certain div needs to be visible. you can achieve this by maintaining the index of currently visible div. when user clicks any button you have to set the index of div to be visible which in the above code is achieved by using setVisibleDiv(index) call. and you can at rendering time use curDivIndex to decide visible div.

like image 37
Jatin Parmar Avatar answered Sep 24 '22 10:09

Jatin Parmar