簡體   English   中英

對 web api 反應 JSX 條件調用

[英]React JSX conditional call to web api

我是 React JS 的初學者。 誰能幫我在 JSX 中編寫一個有條件的 API 調用。 我正在嘗試以下,但它不工作

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);
                });
        });
}

}

您以錯誤的方式聲明 state。 您需要這樣的更新組件:

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

GetOrPost 是值, setGetOrPost 是 function 以更改GetOrPost的值。 你可以在這里閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM