asd123
asd123

Reputation: 15

React setLoading onClick

I have this function.

const handleSubmit = async () => {
  event.preventDefault();
  setLoading(true);
  
  try {
      await axios.get('/something');
  } catch {
      setLoading(false);
  };
 
 };

And I want to setLoad onClick. However, doing that I also rerender the page, and I lost all my form data. Putting inside the try will not cause the expected effect.

Tried with useRef, but that doesn't make the form data disappear but neither the loading appear, 'cause again: the page doesn't rerender.

Any solution?

Upvotes: 0

Views: 302

Answers (1)

Uzair Asif
Uzair Asif

Reputation: 11

You forgot to put event as function parameter

const handleSubmit = async (event) => {
  event.preventDefault();
  setLoading(true);
  
  try {
      await axios.get('/something');
  } catch {
      setLoading(false);
  };
 
};

Upvotes: 1

Related Questions