0

I am beginner in a React JS. Can anyone help me to write a conditional API call in JSX. I am trying the following and it's not working

const GetOrPost = useState(true);

const GetOrPostDataFeed = (formdata)=>{

if(GetOrPost)
{
acquireToken().then((response) => {
                fetch(window.location.origin + '/api/file', { method: 'POST', headers: { Authorization: 'Bearer ' + response.accessToken }, body: formData })
                    .then(response => response.json())
                    .then(data => { console.log('Success:', data);  })
                    .catch((error) => {
                        console.error('Error:', error);
                        trackError(error);
                         });
                    });
}
else
{
acquireToken().then((response) => {
            fetch(window.location.origin + '/api/GetDetail/' + id)
                .then(response => response.json())
                .then(data => { console.log('Success:', data);  })
                .catch((error) => {
                    console.error('Error:', error);
                    trackError(error);
                });
        });
}

}
3
  • Any errors in your console? Do you see the request in your network tab? Commented May 7, 2021 at 10:34
  • 1
    My apologies, that is my typos here. correct it. Commented May 7, 2021 at 10:36
  • Please consult the official react documentation for useState to ensure you used the feature with the correct syntax before posting to Stackoverflow. Commented May 7, 2021 at 10:46

1 Answer 1

1

You are declaring state wrong way. You need update component like this:

const [GetOrPost, setGetOrPost ] = useState(true);

GetOrPost is the value and setGetOrPost is the function to change value of GetOrPost. You can read more in here

Sign up to request clarification or add additional context in comments.

Comments

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.