Need little help on React hooks

khanh tung

I try to make a todo list with React and Redux Toolkit. In handleSubmit function to add item on list, my setText somehow not set the value to empty string. Here are my full code on Stackblitz: https://stackblitz.com/edit/react-ts-bqmjz1?file=components%2FTodoForm.tsx

const TodoForm = (): JSX.Element => {
  //Input value
  const [text, setText] = useState('');

  const dispatch = useDispatch();

  //setText not working
  const handleSubmit = (e: any) => {
    e.preventDefault();
    if (text.trim().length === 0) {
      return;
    }
    dispatch(addItem({ title: text }));
    setText('');
  };

  return (
    <form className="container-fluid" onSubmit={handleSubmit}>
      <div className="input-group">
        <input
          className="form-control"
          placeholder="Enter the value..."
          onChange={(e: { target: HTMLInputElement }) =>
            setText(e.target.value)
          }
        />
        {/* Submit button */}
        <button type="submit" className="btn btn-primary">
          Add
        </button>
      </div>
    </form>
  );
};
bopbopbop

You're very close! You just missed the value prop on the input:

value={text}

const TodoForm = (): JSX.Element => {
  //Input value
  const [text, setText] = useState('');

  const dispatch = useDispatch();

  //setText not working
  const handleSubmit = (e: any) => {
    e.preventDefault();
    if (text.trim().length === 0) {
      return;
    }
    dispatch(addItem({ title: text }));
    setText('');
  };

  return (
    <form className="container-fluid" onSubmit={handleSubmit}>
      <div className="input-group">
        <input
          className="form-control"
          placeholder="Enter the value..."
          value={text}
          onChange={(e: { target: HTMLInputElement }) =>
            setText(e.target.value)
          }
        />
        {/* Submit button */}
        <button type="submit" className="btn btn-primary">
          Add
        </button>
      </div>
    </form>
  );
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Need help in converting this code to functional hooks from class component (React)

Just need a little help debugging my code

React/Redux: Need help render API Response

React/React-Hooks: Need to run a function onLoad within React Hooks component

Need help implementing RNCamera in a react native project

Need help in logic building for react table

react hooks help refactor from class based component

need help getting props to work using react

Need help to get started with React

Need a little help to fix an Arduino RFID program

Need little help php mysql

Need a little help, in hiding/disabling the jbutton

Need help new to programming using Python and I am a little confused, its more like a really confused

need a little help finishing this code

Need a little bit of help finishing a generic MySQL select method

You just need to think a little bit to help me find the solution

Need help on React Js

Need a little regex help in python

Need a little help populating in Excel

Need little help on understanding how starting index and substring works

I need help in converting react class component to react hooks

React Need help Syncfusion data grid

Need Help Refactoring a React Component

Need Help Figuring Out State in React

Need a little help getting my title screen to appear

Need help in creating a selectable Card React component

I need some help at my script (c#) with little lines of code or explanation for math formula

Need minor help in react redux

I need some help for a little SwiftUI animation