0

I've got this React app which sends a 'Post' request. The problem is that: i'm using useEffect(() to automate the sending process of my messages and the application is not capturing the "Authorization Bearer token***!@#!!@$#$" and as a consequence i received 400 http error. As far as i just i click the send button the return is: 200 ok. Something even more stange is that everytime i initialize this React app and write something wrong inside my code intentionally and right after i fix the line which i wrote to cause the error msg, my useEffect() works perfectly without the necessity of press send button. And this is the behavior i'm waiting for. I don't understand why the application only works perfectly when i update some code into the Javascript or when i click the button. Have anyone seen something like this? Here is my useEffect():

useEffect(() => {
     
  const handleClick = () => {
  setInterval(() => {
   try {
      handleOnDataTrigger();
   catch (error) {
      console.error('Error in handleOnInputSend:', error);
   }
   , 20000);
  };
   handleClick();
  []);

And this is the excerpt when the app makes the POST request:

try {
      const response = await axios({
        url: url,
        method: reqMethod,
        params: convertKeyValueToObject(queryParams),
        headers: convertKeyValueToObject(headers),
        data,
      });

        

console.log("Result of post:\n");
        console.log(response);
                                
        setResponse(response);
  
 catch (e) {
   console.log(e);
  setResponse(e);

I'll provide more information soon. But i'm really having a hard time with this problem and didn't find any solution yet.

7
  • Are able to see the auth token if you console it within the useEffect? Commented May 9, 2024 at 18:21
  • Oh yes, i can see it. But only when i update some code into the Javascript or when i click the Send button. It does not work when the app initializes. Commented May 9, 2024 at 18:30
  • 1
    You've passed an empty dependency array to your useEffect so it will only ever run once on mount. (changing a line in your code is probably just triggering hot-reloading to re-mount the component, thus running the useEffect again). I see you are setting an interval, but setInterval does not play well with async, and you are not cleaning up your interval so you're going to end up with more and more requests running in the background. Commented May 9, 2024 at 22:51
  • Does this answer your question? React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing Commented May 9, 2024 at 22:55
  • I tried putting handleOnDataTrigger inside the array, but it runs without stop and i need an interval at least 10 seconds, that's why i left empty. Commented May 10, 2024 at 2:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.