https://scotch.io/tutorials/create-a-custom-usefetch-react-hookの例に従って、フェッチを実行します。すべての作品。
https://stackoverflow.com/a/29823632に従ってPOST'Accept': 'application/json'
を作成するように少し変更します。ただし、どのように投稿しても、テキスト/Html コンテキスト タイプのままです。'Content-Type': 'application/json'
const useFetch = (url, body) => {
const [response, setResponse] = React.useState(null);
const [error, setError] = React.useState(null);
React.useEffect(() => {
const FetchData = async () => {
try {
const method = "POST"
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
const options = {
method,
headers
};
if (body) options.body = JSON.stringify(body);
const res = await fetch(url, options);
const json = await res.json();
setResponse(json);
} catch (error) {
setError(error);
}
};
FetchData();
}, []);
return { response, error };
};
export default useFetch
私は何を間違えましたか?