Javascript ReactJS onClick setState 到不同的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38038521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:00:48  来源:igfitidea点击:

ReactJS onClick setState to different element

javascriptreactjs

提问by xoomer

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

所以问题是我无法使用 onCLick 函数触发我想要的元素。现在我正在尝试删除导航

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!

谢谢你!

采纳答案by 1ven

Only react components have setStatemethod. Working example:

只有反应组件才有setState方法。工作示例:

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 rendermethod.

此外,您应该在构造函数而不是render方法中绑定事件处理程序。

回答by Piyush.kapoor

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>
    );
}
}

回答by YasirAzgar

You can also use inline functions,

您还可以使用内联函数,

<nav onClick={() => this.setState(navStatus: "navShow")}>

but some argue that it has performance issue, https://medium.com/front-end-weekly/dont-use-inline-arrow-functions-as-properties-e3cae2680b9d

但有人认为它有性能问题,https://medium.com/front-end-weekly/dont-use-inline-arrow-functions-as-properties-e3cae2680b9d