Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS onClick setState to different element

I am new to react, and I am having a little issue. Maybe somebody can help me out.

So the issue is that I am unable to triger the element I want with onCLick function. Now I am trying to remove the navigation when

import React from "react";
import ReactDOM from "react-dom";

export default class Nav extends React.Component {
    constructor() {
        super();
        this.state = {navStatus: "navHide"};
    }

    navClose() {
        var navOpened = document.getElementById("myNav");
        navOpened.setState({navStatus: "navHide"});
    }

    navOpen() {
        this.setState({navStatus: "navShow"});
    }

    render() {
        return(
            <nav onClick={this.navOpen.bind(this)}>
                <div id="myNav" className={this.state.navStatus}>
                    <div className="navClose" onClick={this.navClose.bind(this)}>
                        <object className="navCloseBtn" type="image/svg+xml" data="svg/close.svg"></object>
                    </div>                  
                </div>
            </nav>
        );
    }
}

The error I am having is

nav.js:12 Uncaught TypeError: navOpened.setState is not a function

Also if you notice some patterns that I could improve in my code, I would really appreciate the feedback.

Thank You!

like image 724
xoomer Avatar asked Jun 26 '16 12:06

xoomer


People also ask

How do I change the onClick state of React component?

We have to set initial state value inside constructor function and set click event handler of the element upon which click, results in changing state. Then pass the function to the click handler and change the state of the component inside the function using setState.

Can I pass setState to another component?

You can't directly call setState on a parent component from a child component because the updating of a component state is restricted to the current component. To handle this, simply pass a function from the parent to the child that contains setState .


2 Answers

Only react components have setState method. Working example:

import React from "react";
import ReactDOM from "react-dom";

export default class Nav extends React.Component {
constructor() {
  super();

  this.state = {
    navStatus: "navHide"
  };

  this.navClose = this.navClose.bind(this);
  this.navOpen = this.navOpen.bind(this);
}

navClose(e) {
  e.stopPropagation();
  this.setState({
    navStatus: "navHide"
  });
}

navOpen() {
  this.setState({
    navStatus: "navShow"
  });
}

render() {
  return(
    <nav onClick={this.navOpen}>
      <div id="myNav" className={this.state.navStatus}>
        <div className="navClose" onClick={this.navClose}>
          <object
            className="navCloseBtn"
            type="image/svg+xml"
            data="svg/close.svg"
          ></object>
        </div>                  
      </div>
    </nav>
  );
}

Also you should bind event handlers in constructor instead of render method.

like image 103
1ven Avatar answered Oct 03 '22 22:10

1ven


import React from "react";
import ReactDOM from "react-dom";

export default class Nav extends React.Component {
constructor() {
    super();
    this.state = {navStatus: "navHide"};
}

navClose() {
    var navOpened = document.getElementById("myNav");
    this.setState({navStatus: "navHide"});
}

navOpen() {
    this.setState({navStatus: "navShow"});
}

render() {
    return(
        <nav onClick={this.navOpen.bind(this)}>
            <div id="myNav" className={this.state.navStatus}>
                <div className="navClose" onClick={this.navClose.bind(this)}>
                    <object className="navCloseBtn" type="image/svg+xml" data="svg/close.svg"></object>
                </div>                  
            </div>
        </nav>
    );
}
}
like image 41
Piyush.kapoor Avatar answered Oct 03 '22 23:10

Piyush.kapoor