简体   繁体   English

ReactJS 的启用-禁用按钮

[英]Enable-Disable button with ReactJS

I am trying to enable or disable a button based on whether or not there is text in my input but cant seem to achieve it.我正在尝试根据我的输入中是否有文本来启用或禁用按钮,但似乎无法实现。 When I manually set {true OR false} in the disabled property of Button function it works fine but I am really confused on how to set that dynamically based on the content of the input.当我在按钮 function 的禁用属性中手动设置 {true OR false} 时,它工作正常,但我真的很困惑如何根据输入内容动态设置它。

Any guidance is super welcome!非常欢迎任何指导!

This is my app code这是我的应用程序代码

 import { useState } from "react"; function Input (props){ const { onChange, value } = props return (<input value={value} onChange={onChange} type="text" placeholder="Add a ToDo" maxLength="50"/>) } function Button (props) { const {onChange, state, text} = props return (<button disabled={false} onChange={onChange}>{text}</button>) } function App() { const [text, setText] = useState(""); const [state, setSate] = useState(true); const handleChange = (event) => { if (.setText(event.target;value)) { setSate(false); } else { setSate(true); } }; return ( <div className="App"> <div className="container"> <Input value={text} onChange={handleChange} /> <Button onChange={() => handleChange(state)} text="Add" /> <Button onChange={() => handleChange(state)} text="Clean" /> </div> ); } export default App;

Button element should change to: Button元素应更改为:

function Button (props) {
    const {disabled, onChange, state, text} = props
    return (<button disabled={disabled} onChange={onChange}>{text}</button>)
}

Rendering of it should change to:它的渲染应更改为:

...
<Button disabled={!text} onChange={() => handleBtn()} text="Add" />
...

Sandbox: https://codesandbox.io/s/zen-hawking-qqzkw?file=/src/App.js沙盒: https://codesandbox.io/s/zen-hawking-qqzkw?file=/src/App.js

The idea is to send down disabled prop which would be true if the there is no text in the field.这个想法是发送disabled的道具,如果字段中没有文本,这将是true的。

ALSO, handleChange should look like this:此外, handleChange应如下所示:

  const handleChange = (event) => {
    setText(event.target.value);
  };

because the rest of your code in that function does not do anything.因为您的代码中的 rest 没有做任何事情。

Buttons should have their own handler functions.eg const handleBtn = () => {};按钮应该有自己的处理函数const handleBtn = () => {};

So you should pass the state value you are using to store whatever the users write in the input to the button so that the button knows when the input has text on it or not.因此,您应该传递 state 值,用于存储用户在按钮输入中写入的任何内容,以便按钮知道输入是否有文本。 And then your second state value can be used to store your todo list, so something like this然后你的第二个 state 值可以用来存储你的待办事项列表,所以像这样

import { useState } from "react";

function Input({ handleChange, value }) {
  return (
    <input
      value={value}
      onChange={handleChange}
      type="text"
      placeholder="Add a Todo"
      maxLength="50"
    />
  );
}

function Button({ handleClick, text, disabled }) {
  return (
    <button disabled={disabled} onClick={handleClick}>
      {text}
    </button>
  );
}

function App() {
  const [value, setValue] = useState("");
  const [todoList, setTodoList] = useState([]);

  const handleChange = (event) => {
    setValue(event.target.value);
  };

  const handleAdd = () => {
    setTodoList([...todoList, value]);
    handleClear();
  };

  const handleClear = () => {
    setValue("");
  };

  return (
    <div className="App">
      <div className="container">
        <Input value={value} handleChange={handleChange} />
        <Button handleClick={handleAdd} disabled={!value} text="Add" />
        <Button handleClick={handleClear} disabled={!value} text="Clear" />
      </div>
    </div>
  );
}

export default App;

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

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