简体   繁体   English

如何在 React.js 中创建动态(条件)占位符

[英]How to create dynamic (conditional) placeholder in React.js

So I have a submit form where the user needs to create a task by typing in a task name.所以我有一个提交表单,用户需要在其中输入任务名称来创建任务。 I want it to be empty at the beginning and have a placeholder of "you must enter a task" when the user click add without entering anything.我希望它在开始时为空,并在用户单击添加而不输入任何内容时有一个“您必须输入任务”的占位符。 Now I can achieve it to display the placeholder but it's either always there or I encounter unreachable code.现在我可以实现它来显示占位符,但它要么一直存在,要么遇到无法访问的代码。 I know how to clean the submission & return to the add function, just need to be able to display the placeholder conditionally.我知道如何清除提交并返回添加 function,只需要能够有条件地显示占位符。 Here's what my code looks like atm:这是我的代码在 atm 中的样子:

import { useState } from "react";

export default function Todos() {
  const [todos, setTodos] = useState([{ text: "hey" }]);
  const [todoText, setTodoText] = useState("");
  const [isEmpty, setEmpty] = useState("false");

  const addTodo = (e) => {
    e.preventDefault();
    if (todoText){
    setTodos([...todos, { text: todoText }]);
    setTodoText("");
    
  } else {
      setEmpty(true)
      setTodoText("");
      return
  }
  
} 

  

  return (
    <div>
      {todos.map((todo, index) => (
        <div key={index}>
          <input type="checkbox" />
          <label>{todo.text}</label>
        </div>
      ))}
      <br />
      <form onSubmit={addTodo}>
        <input
          
          value={todoText}
          onChange={(e) => setTodoText(e.target.value)}
          type="text"
        ></input>
        <button type="submit">Add</button>
        {isEmpty &&<span style={{ color: "red" }}>Enter a task</span>}
      </form>
    </div>
  );
}

I could change your code with the following:我可以通过以下方式更改您的代码:

You need to initialize isEmpty by false instead of string "false" .您需要通过false而不是字符串"false"来初始化isEmpty

And you can use this flag on showing placeholder texts.您可以在显示占位符文本时使用此标志。

Note that I renamed isEmpty by showError .请注意,我将isEmpty重命名为showError

import { useState } from "react";

export default function Todos() {
  const [todos, setTodos] = useState([{text: "hey"}]);
  const [todoText, setTodoText] = useState("");
  const [showError, setShowError] = useState(false);

  // @ts-ignore
  const addTodo = (e) => {
    e.preventDefault();

    if (todoText) {
      setTodos([...todos, {text: todoText}]);
      setTodoText("");
      setShowError(false);
    } else {
      setTodoText("");
      setShowError(true);
      return
    }
  }

  return (
    <div>
      {todos.map((todo, index) => (
        <div key={index}>
          <input type="checkbox"/>
          <label>{todo.text}</label>
        </div>
      ))}
      <br/>
      <form onSubmit={addTodo}>
        <input
          value={todoText}
          onChange={(e) => setTodoText(e.target.value)}
          type="text"
        ></input>
        <button type="submit">Add</button>
        {(showError && !todoText) && <span style={{color: "red"}}>Enter a task</span>}
      </form>
    </div>
  );
}

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

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