简体   繁体   English

根据 ReactJs 中的请求获取数据

[英]Fetch data depending by request in ReactJs

I want to send the http request what will depend by the button what was clicked.我想发送 http 请求,这取决于单击的按钮。 So, bellow is my fetch and it is working:所以,波纹管是我的提取物,它正在工作:

 //children component const Post = (props) => { function sendRequestToParrent() { props.rRequests("POST") } return ( <div> <Button onClick={() => { sendRequestToParrent(); }} type='submit'>POST DATA</Button> </div> ); }; export default Post; //parent component const [variable, setVariable ] = useState(''); // here i set the data from child component which was received with a callback. function rRequests(request) { setVariable (request); return ( fetch("URL", { method: `${variable}`, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ .... }) }).then(res => { console.log("response", res); }) ) } return ( <div> <Post rRequests={rRequests} /> </div> )

The issue appear when i change the method from method: POST to method: ${variable}`.当我将方法从method: POST更改为method: ${variable}` 时出现问题。 It is possible to add a variable inside method and depending by what value that variable will have, to get the response?可以在方法内部添加一个变量,并根据该变量将具有的值来获得响应?

For example: if i will click a button the variable will be equal with "POST", when i will click on another button the variable will be equal with 'DELETE' and depending by this to get the response.例如:如果我单击一个按钮,该变量将等于“POST”,当我单击另一个按钮时,该变量将等于“DELETE”,并由此获得响应。

This is a classic case of the asynchronous nature of setVariable.这是 setVariable 异步性质的典型案例。 You are setting the variable and then immediately using it.您正在设置变量,然后立即使用它。 As the setting of any state happends asynchronously, that is why it is still empty where it being assigned to method.由于任何状态的设置都是异步发生的,这就是为什么它在被分配给方法的地方仍然是空的。 You can directly assign the value of request that you are passing as the parameter to the function.您可以直接将作为参数传递给函数的请求的值分配给函数。

 function rRequests(request) { setVariable (request); return ( fetch("URL", { method: `${request}`, headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: JSON.stringify({ .... }) }).then(res => { console.log("response", res); }) ) }

You can use the new variable in useEffect hook if you want to如果你想,你可以在 useEffect 钩子中使用新变量

remove template literal from the method从方法中删除模板文字

You need to set the incoming request type from the higher component.您需要设置来自更高组件的传入请求类型。 "" is showing because of the default parameter you are initiating.由于您正在启动的默认参数而显示“”。

const [variable, setVariable ] = useState(''); // here i set the data from child component which was received with a callback. 

setVariable(this.props.method); // method is the parameter name you are passing from the parent component

function submit() {
    return fetch("URL", {
                method: variable,
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                },
                body: JSON.stringify({ .... })
            }).then(res => {
                console.log("response", res);
            });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM