简体   繁体   English

反应 onBlur setState 不起作用(jsbin)

[英]react onBlur setState doesn't work (jsbin)

I've been struggling with debugging react.我一直在努力调试反应。 In jsbin, there's no way I can know what error it is, when I open the console or console of my broswer there's no clear indication of what my error is about.在 jsbin 中,我无法知道它是什么错误,当我打开我的浏览器的控制台或控制台时,没有明确指示我的错误是关于什么的。

http://jsbin.com/doletanole/1/edit?html,js,console,output http://jsbin.com/doletanole/1/edit?html,js,console,output

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.getInput = this.focusHandler.bind(this)
    this.state = {hasError:false}
  }
  focusHandler(e) {
    if(e.target.value === ''){
      this.setState({hasError:true})
    }
  }
  render() {
    return (      
      <input placeholder="username" type="text" onBlur={this.focusHandler}/>
{this.state.hasError ? <span>Username is required</span> :  ''}  
    );
  }
}

Any better way to debug react ?有什么更好的方法来调试反应吗? I just want to show the error msg if when the user go away of the input base on the state.如果当用户离开基于状态的输入时,我只想显示错误消息。

Whenever binding the methods in the constructor, try to use the same name to avoid these kind of mistakes, i think you need to reset the state value to false if the username is not blank, Try this Code:每当在构造函数中绑定方法时,尽量使用相同的名称来避免此类错误,如果username不为空,我认为您需要将状态值重置为false ,请尝试以下代码:

class HelloWorldComponent extends React.Component {
  constructor() {
    super()
    this.focusHandler = this.focusHandler.bind(this)
    this.state = {hasError:false}
  }
  focusHandler(e) {
    this.setState({hasError: e.target.value != '' ? false : true});
  }
  render() {
    return (    
      <div>  
         <input placeholder="username" type="text" onBlur={this.focusHandler}/>
         {this.state.hasError ? <span>Username is required</span> :  ''}  
      </div>
     );
  }
}

Check working example: http://jsbin.com/cozenariqo/1/edit?html,js,console,output检查工作示例: http : //jsbin.com/cozenariqo/1/edit?html,js,console,output

First of all, you must return only one top level element from a component (or an array, but that's less common).首先,您必须仅从组件(或数组,但不太常见)中返回一个顶级元素。 Wrap your rendered output in a single element:将渲染输出包装在单个元素中:

render() {
    return (
        <div>
            <input placeholder="username" type="text" onBlur={this.focusHandler}/>
            {this.state.hasError ? <span>Username is required</span> :  ''}  
        </div>
    );
}

Secondly, you're not correctly binding the focusHandler event.其次,您没有正确绑定 focusHandler 事件。 Change it to onBlur={this.focusHandler.bind(this)} .将其更改为onBlur={this.focusHandler.bind(this)} Suggested reading:React, ES6, Autobinding, and createClass()推荐阅读:React、ES6、Autobinding 和 createClass()

The error blocking your code from loading was from the wrapping element.阻止您的代码加载的错误来自包装元素。 JS Bin does not propagate Babel errors to the user well. JS Bin 不会很好地将 Babel 错误传播给用户。 I would suggest not using it and set up a local development environment with Babel and Webpack instead.我建议不要使用它,而是使用 Babel 和 Webpack 设置本地开发环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM