在reactjs中使用常量发送获取参数

在reactjs中使用常量发送获取参数,reactjs,get,fetch,Reactjs,Get,Fetch,我正在执行这样的获取请求: let [responseData, setResponseData] = React.useState(''); const dispatch = useDispatch(); const fetchData = React.useCallback(() => { let headers = new Headers({ 'Content-Type': 'application/json',

我正在执行这样的获取请求:

let [responseData, setResponseData] = React.useState('');
    const dispatch = useDispatch();
    const fetchData = React.useCallback(() => {
        let headers = new Headers({
            'Content-Type': 'application/json',
        });
        fetch('http://localhost:3000/current_user', {
            method: 'GET',
            mode: 'cors',
            headers: headers,
            cache: 'no-cache',
            redirect: 'follow',
            referrer: 'no-referrer',
            credentials: 'include',
        })
            .then(response => {
                if (response.ok) return response.json();
                throw new Error('Request failed.');
            })
            .then(data => {
                setResponseData(data); // sent user data to redux
                dispatch(props.setUser(data));
            })
            .catch(error => {
                console.log(error);
            });
    }, []);
    React.useEffect(() => {
        fetchData();
    }, [fetchData]);
let [responseData, setResponseData] = React.useState('');
    const dispatch = useDispatch();
    const fetchData = React.useCallback(() => {
        let headers = new Headers({
            'Content-Type': 'application/json',
        });
        const reqParams = {
            method: 'GET',
            mode: 'cors',
            headers: headers,
            cache: 'no-cache',
            redirect: 'follow',
            referrer: 'no-referrer',
            credentials: 'include',
        }
        fetch('http://localhost:3000/current_user', {
           reqParams, 
        })
            .then(response => {
                if (response.ok) return response.json();
                throw new Error('Request failed.');
            })
            .then(data => {
                setResponseData(data); // sent user data to redux
                dispatch(props.setUser(data));
            })
            .catch(error => {
                console.log(error);
            });
    }, []);
    React.useEffect(() => {
        fetchData();
    }, [fetchData]);
但我想将所有的fetch请求参数放在一个常量中,并在fetch函数中调用该常量,如下所示:

let [responseData, setResponseData] = React.useState('');
    const dispatch = useDispatch();
    const fetchData = React.useCallback(() => {
        let headers = new Headers({
            'Content-Type': 'application/json',
        });
        fetch('http://localhost:3000/current_user', {
            method: 'GET',
            mode: 'cors',
            headers: headers,
            cache: 'no-cache',
            redirect: 'follow',
            referrer: 'no-referrer',
            credentials: 'include',
        })
            .then(response => {
                if (response.ok) return response.json();
                throw new Error('Request failed.');
            })
            .then(data => {
                setResponseData(data); // sent user data to redux
                dispatch(props.setUser(data));
            })
            .catch(error => {
                console.log(error);
            });
    }, []);
    React.useEffect(() => {
        fetchData();
    }, [fetchData]);
let [responseData, setResponseData] = React.useState('');
    const dispatch = useDispatch();
    const fetchData = React.useCallback(() => {
        let headers = new Headers({
            'Content-Type': 'application/json',
        });
        const reqParams = {
            method: 'GET',
            mode: 'cors',
            headers: headers,
            cache: 'no-cache',
            redirect: 'follow',
            referrer: 'no-referrer',
            credentials: 'include',
        }
        fetch('http://localhost:3000/current_user', {
           reqParams, 
        })
            .then(response => {
                if (response.ok) return response.json();
                throw new Error('Request failed.');
            })
            .then(data => {
                setResponseData(data); // sent user data to redux
                dispatch(props.setUser(data));
            })
            .catch(error => {
                console.log(error);
            });
    }, []);
    React.useEffect(() => {
        fetchData();
    }, [fetchData]);

但这并不是一个成功的获取调用。我做错了什么,我不能找出这里的错误。如何正确编写该函数。

它不起作用,因为参数嵌套在
reqParams
键下

应该是

fetch('http://localhost:3000/current_user,reqParams)
注意

fetch('http://localhost:3000/current_user', {
reqParams,
})

fetch('http://localhost:3000/current_user', {
要求参数:{
方法:“GET”,
模式:“cors”,
标题,
缓存:“无缓存”,
重定向:“跟随”,
推荐人:“无推荐人”,
凭据:“包括”,
}
})

它不起作用,因为参数嵌套在
reqParams
键下

应该是

fetch('http://localhost:3000/current_user,reqParams)
注意

fetch('http://localhost:3000/current_user', {
reqParams,
})

fetch('http://localhost:3000/current_user', {
要求参数:{
方法:“GET”,
模式:“cors”,
标题,
缓存:“无缓存”,
重定向:“跟随”,
推荐人:“无推荐人”,
凭据:“包括”,
}
})
似乎是因为
fetch('http://localhost:3000/current_user“,{reqParams,})
此行。你试过了吗http://localhost:3000/current_user,reqParams)?似乎是因为
fetch('http://localhost:3000/current_user“,{reqParams,})
此行。你试过了吗http://localhost:3000/current_user,reqParams)?