Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How do I target and change the style of a different element when I click a button in react

class App extends Component {
  constructor() {
    super();
    this.state = { hidden: false };
  }

  handleFinish = () => {
    console.log("CLickedddd");
    this.setState({ hidden: !this.state.hidden });
  };

  render() {
    return (
      <>
        <button id="finish" onClick={this.handleFinish}>
          Finish
        </button>
        <div
          style={{ height: 300, width: 300, backgroundColor: "red" }}
          className={this.state.hidden ? "hidden" : ""}
        ></div>
      </>
    );
  }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #I #target #change #style #element #I #click #button #react
ADD COMMENT
Topic
Name
7+6 =