1

I want to toggleclass name of one element by clicking on another element. Both elements are in separate component files. I don't know how to get the state of an element and pass it to another element. Please help me solving the problem.

file1.js

<Button onClick={this.toggleFunction}>Button</Button>

file2.js

<div class="wrapper"></div>

I want to toggle class active on wrapper div when the button is clicked.

Thanks

10
  • Could you include the code you have written so far? Are the components siblings, or are they parent/child? Commented Aug 10, 2018 at 14:05
  • The components are parent/child Commented Aug 10, 2018 at 14:07
  • So you want to toggle a class in the parent when a button in the child is clicked? Commented Aug 10, 2018 at 14:09
  • codesandbox.io/s/m4py2y97zp Commented Aug 10, 2018 at 14:11
  • 1
    @Tholle I want to change class of a parent wrapper by clicking button in the child. Commented Aug 10, 2018 at 14:15

2 Answers 2

3

class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.addActiveClass= this.addActiveClass.bind(this);
        this.state = {
            active: false,
        };
    }
    toggleClass() {
        const currentState = this.state.active;
        this.setState({ active: !currentState });
    };

    render() {
        return (
            <div 
                className={this.state.active ? 'your_className': null} 
                onClick={this.toggleClass} 
            >
                <p>{this.props.text}</p>
            </div>
        )
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

The code runs on the same file. I want to apply changes from 1 file to another. My button is in header.js and I want to apply change in base.js file.
1
Parent Component 
        import React from "react";
        import ButtonComponent from "./buttonComponent";
        import "./demo.css";

        //Parent  Component
        class Demo extends React.Component {
          constructor(props) {
            super(props);
            this.state = {
              active: false
            };
          }

          updateValue = value => {
            this.setState({
              active: value
            });
          };
          render() {
            return (
              <div>
                <ButtonComponent updateParent={this.updateValue} />
                <div
                  className={
                    this.state.active ? "dropdownbutton1" : "dropdownbutton1Active"
                  }
                >
                  <label>First</label>
                  <br />
                  <select>
                    <option value="yes">yes</option>
                    <option value="no">no</option>
                  </select>
                </div>
              </div>
            );
          }
        }

        export default Demo;
Child Component 

        import React from "react";
        import ToggleButton from "react-toggle-button";
        import "./demo.css";

        class ButtonComponent extends React.Component {
          constructor(props) {
            super(props);
            this.state = {
              active: false,
              defaultValue: 1
            };
          }

          togglebutton = () => {
            this.props.updateParent(this.state.active);
            this.setState({ active: !this.state.active });
            if (this.state.active) {
              this.setState({ defaultValue: 1 });
            } else {
              this.setState({ defaultValue: -1 });
            }
          };
          render() {
            return (
              <div>
                <div className="ToggleButton">
                  <ToggleButton onClick={this.togglebutton} value={this.state.active} />
                </div>
              </div>
            );
          }
        }

        export default ButtonComponent;

Link :https://codesandbox.io/s/m4py2y97zp

6 Comments

Please don't delete this link.
Hello, Can you add the normal button instead of switch button ?
Changing <ToogleButton onClick={this.togglebutton} value={this.state.active} /> to <button onClick={this.togglebutton} value={this.state.active} >Click Me</button> Should work
I am getting below error Unexpected token (16:18) 14 | } 15 | > 16 | updateValue = value => { | ^ 17 | this.setState({ 18 | active: value 19 | }); errors @ client:167 onmessage @ socket.js:41 EventTarget.dispatchEvent @ sockjs.js:170 (anonymous) @ sockjs.js:883 SockJS._transportMessage @ sockjs.js:881 EventEmitter.emit @ sockjs.js:86 WebSocketTransport.ws.onmessage @ sockjs.js:2957
can you please help me out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.