Home > Enterprise >  how to clear all inputs in react div
how to clear all inputs in react div

Time:06-07

import "./App.css";
import { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { addUser} from "./features/Users";
function App() {
  const dispatch = useDispatch();
  const userList = useSelector((state) => state.users.value);

  const [name, setName] = useState("");
  const [username, setUsername] = useState("");

  return (
    <div className="App">
      {" "}
      <div className="addUser">
        <input
          type="text"
          placeholder="Name..."
          onChange={(event) => {
            setName(event.target.value);
          }}
        />
        <input
          type="text"
          placeholder="Username..."
          onChange={(event) => {
            setUsername(event.target.value);
          }}
        />
        <button
          onClick={() => {
            dispatch(
              addUser({
                id: userList[userList.length - 1].id   1,
                name,
                username,
              })
            );
          }}
        >
          {" "}
          Add User
        </button>
      </div>
);}

I am new to react and redux. After clicking the "Add User" button, new User data from inputs in the code will be added to the backend list. I want the values in input sections to be cleared after clicking the "Add User" button, but I don't know how to do.

CodePudding user response:

you need to clear your state after click on submit button. for ex: set function like =>

const clearData = {
    setName("")
    setUsername("")
    }
 and pass the func to your onClick event.
onClick={clearData}

CodePudding user response:

The following code will work perfectly fine.

Just assign value={name} and value={username} to both input types respectively and when you click Add User just clear the data in both the states.

import "./App.css";
import { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { addUser} from "./features/Users";
function App() {
  const dispatch = useDispatch();
  const userList = useSelector((state) => state.users.value);

  const [name, setName] = useState("");
  const [username, setUsername] = useState("");

  return (
    <div className="App">
      {" "}
      <div className="addUser">
        <input
          type="text"
          placeholder="Name..."
          value={name}
          onChange={(event) => {
            setName(event.target.value);
          }}
        />
        <input
          type="text"
          placeholder="Username..."
          value={username}
          onChange={(event) => {
            setUsername(event.target.value);
          }}
        />
        <button
          onClick={() => {
            setName("");
            setUsername("");
            dispatch(
              addUser({
                id: userList[userList.length - 1].id   1,
                name,
                username,
              })
            );
          }}
        >
          {" "}
          Add User
        </button>
      </div>
);}

CodePudding user response:

You can maintain a simple variable with list of form fields and can update the form state with the variable when you needed to clear form data. The below approach comes handy when you need to add additional fields as well.

import "./App.css";
import { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { addUser} from "./features/Users";

const formFields = { name: '', username: '' };

function App() {
  const dispatch = useDispatch();
  const userList = useSelector((state) => state.users.value);
  const [params, setParams] = useState(formFields)

  const handleChange = (e) => {
    const { name, value } = e.target;
    setParams({ ...params }, ...{[name]: value});
  }

  const clearForm = () => setParams(formFields);

  return (
    <div className="App">
      <div className="addUser">
        <input
          type="text"
          placeholder="Name..."
          value={params.name}
          onChange={(e) => handleChange(e)}
        />
        <input
          type="text"
          placeholder="Username..."
          value={params.username}
          onChange={(e) => handleChange(e)}
        />
        <button
          onClick={() => {
            dispatch(
              addUser({
                id: userList[userList.length - 1].id   1,
                ...params
              })
            );
            clearForm();
          }}
        >
          {" "}
          Add User
        </button>
      </div>
    </div>
)
}
  • Related