Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React toggle component

I have this simple code below. When I press the Toggle Button the component Child should hide/show, but it's not.

Do I have to re-render something? I don't want to switch in/out a CSS class, just toggle via a button click

import React, {Component} from 'react';

let active = true

const handleClick = () => {
    active = !active
}

class Parent extends React.Component {
    render() {      
        return (    
            <div>  
                <OtherComponent />

                {active && <Child />}

                <button type="button" onClick={handleClick}>
                    Toggle
                </button>

            </div>            
        )           
    }
}

class Child extends React.Component {
    render() {

        return (    
            <div>          
                I am the child
            </div>            
        )             
    }
}

class OtherComponent extends React.Component {
    render() {       
        return (    
            <div>          
                I am the OtherComponent
            </div>            
        )           
    }
}
like image 343
Rory Avatar asked May 18 '17 13:05

Rory


People also ask

How do you toggle component in React?

Replace the contents of src/ToggleSwitch/ToggleSwitch. js with the following: import React from "react"; import PropTypes from "prop-types"; import './ToggleSwitch. scss'; /* Toggle Switch Component Note: id, checked and onChange are required for ToggleSwitch component to function.

How do you write toggle function in React?

If we want to implement toggle functionality to a button, then we can have states in our component that will either be true or false and based on the value of state we can implement toggle functionality. When we click on the button and the current value of the state is true then we changed it to false and vice versa.

What is toggle in react JS?

In this tutorial, we will see how to toggle elements in React js. Toggling simply means to show and hide the element alternatively with each click of the button. The same concept is required in many applications like showing and hiding notifications.

How do I toggle checkbox in ReactJS?

Using setState with React Checkbox onChangeToggle the text of a paragraph with the checkbox using the 'useState' hook. */ import React, {useState} from 'react'; function Checkbox() { const [checked, setChecked] = useState(false); const handleChange = () => { setChecked(!


1 Answers

You need to get or set it via state:

class Parent extends React.Component {
    constructor(props, context) {
        super(props, context);

        this.state = {
            active: true,
        };

        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        this.setState({
            active: !this.state.active
        });
    }

    render() {
        return (
            <div>
                <OtherComponent />

                {this.state.active && <Child />}

                <button type="button" onClick={this.handleClick}>
                    Toggle
                </button>

            </div>
        )
    }
}

Note that with this approach you will re:render the entire parent component (as well as it's children).
Consider using another approach, when you are passing a prop to the child component and it will render itself with content based on this prop (it can render an empty div or something).
There are number of libraries that make this job easy for you, like react-collapse with animations and stuff.

like image 58
Sagiv b.g Avatar answered Sep 23 '22 04:09

Sagiv b.g