Skip to content

How can i setState of object based on the condition #189

Locked Answered by Deepak8717
Deepak8717 asked this question in Q&A
Discussion options

You must be logged in to vote

yes , here it goes. I am making sure that my code is clean, concise and minimal. That's why its taking so long

const [values, setValues] = useState({
    name: "",
    email: "",
    password: "",
    confirmPassword: "",
    errors: {
      name: "",
      email: "",
      password: "",
      confirmPassword: "",
    },
  });

const submitHandler = (e) => {
    e.preventDefault();
    Object.entries(values).map((value) => {
      if (isEmpty(value)) {
        setValues((prevState) => ({
          ...prevState,
          errors: {
            ...prevState.errors,
            [value[0]]: `Please fill the ${value[0]}`,
          },
        }));
      }
    });
  };

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Comment options

Deepak8717
May 18, 2022
Collaborator Author

You must be logged in to vote
1 reply
@atapas
Comment options

Comment options

Deepak8717
May 18, 2022
Collaborator Author

You must be logged in to vote
6 replies
@atapas
Comment options

@Deepak8717
Comment options

Deepak8717 May 18, 2022
Collaborator Author

@atapas
Comment options

@Deepak8717
Comment options

Deepak8717 May 18, 2022
Collaborator Author

@atapas
Comment options

Answer selected by atapas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants