简体   繁体   English

ReactJS-编写函数以启用输入文本验证的按钮

[英]Reactjs - writing a function to enable a button on validation of input text

I am working on a reactjs application that has a save button - that is disabled until valid user text is entered. 我正在使用带有保存按钮的reactjs应用程序-在输入有效的用户文本之前将其禁用。

I have something like this currently in place 我现在有这样的东西

disabled={variable.length > 0}

but I want something more like this 但我想要更多这样的东西

disabled={(this.isValid(variable)}

however - it doesn't appear to be invoked. 但是-它似乎没有被调用。

I've added the function outside of the render - mocked as true for now 我已经在渲染器之外添加了该功能-暂时模拟为true

  isValid(value) {
      console.log("value", value);
      return true;
  }

do I have to do a bind this? 我必须做一个绑定吗? add it to the constructor method? 将其添加到构造方法中?

Are you updating "variable" in your state onChange? 您是否正在onChange状态下更新“变量”? You may also consider using onBlur (when users exits the input). 您也可以考虑使用onBlur(当用户退出输入时)。 Also you have syntax error at {(this.isValid(variable)} you have a "(" after "{" that is not closed. 同样,您在{(this.isValid(variable)}处遇到语法错误,在“ {”之后有一个“(”未关闭。

Working example: 工作示例:

import ReactDOM from "react-dom";
import React, { Component } from "react";

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: ""
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.setState({
      text: e.target.value
    });
  }

  isValid(text) {
    return text.length > 0;
  }

  render() {
    return (
      <div>
          <input value={this.state.text} onChange={e => this.handleChange(e)} />
          <button disabled={!this.isValid(this.state.text)}>Add</button>
      </div>
    );
  }
}

ReactDOM.render(<Test />, document.getElementById("root"));

I think the problem is here: disabled={(this.isValid(variable)} there is a parenthesis left at the beginning. 我认为问题出在这里: disabled={(this.isValid(variable)}在开始时还有一个括号。

but I'll pass two examples, one using class and one using hooks: 但我将传递两个示例,一个使用类,另一个使用钩子:

import React, { Component } from "react";

export default class ExemploWithClassComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      variable: ""
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(e) {
    this.setState({
      variable: e.target.value
    });
  }

  isValid(value) {
    return value.length > 0;
  }

  render() {
    return (
      <>
        <input
          value={this.state.variable}
          onChange={e => this.handleChange(e)}
        />
        <button disabled={!this.isValid(this.state.variable)}>Enter</button>
      </>
    );
  }
}

With Hooks ;) 带钩;)

import React, { useState } from "react";

export default function ExemploWithHooksComponent() {
  const [variable, setVariable] = useState("");

  function handleChange(e) {
    setVariable(e.target.value);
  }

  function isValid(value) {
    return value.length > 0;
  }

  return (
    <>
      <input value={variable} onChange={handleChange} />
      <button disabled={!isValid(variable)}>Enter</button>
    </>
  );
}

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

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