参数从一个api传递到react-native [英] Parameter pass from one api to another in react-native

查看:84
本文介绍了参数从一个api传递到react-native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数值从一个API请求传递到第二个API请求,以便第二个api显示结果相应:这是我的函数componentWillMount:

I want to pass a parameter value from one API-Request to 2nd API-request so that 2nd api display result accordingly: Here is my function componentWillMount:

componentWillMount() {
  axios.post('https://APISITE/api/Auth/AuthorizeByApplication?applicationId=b72fc47a-ef82-4cb3-8179-2113f09c50ff&applicationSecret=e727f554-7d27-4fd2-bcaf-dad3e0079821&token=cd431b31abd667bbb1e947be42077e9d')
  .then((response) => { console.log(response.data); });

      axios.get('https://APISITE//api/Stock/GetStockItems',

        { 
            params: {
                keyWord: 454534534543,
                locationId: '',
                entriesPerPage: 100000,
                pageNumber: 1,
                excludeComposites: true,
                //add other params
            },
            headers: 
            { Authorization: 'asdfasdsfdfdfdfsfsdxxx' 
            }
        //}).then((response) => { console.log(response.data); }); 
         }).then((response) => this.setState({ products: response.data }));

         axios.get('https://APISITE//api/Stock/GetStockLevel', {  

            params: { 
                stockItemId: '2f80b45c-85ff-449b-9ad6-ffcc4bb640dd',
                 },
                   headers: 
                   { Authorization: 'asdfasdsfdfdfdfsfsdxxx'
                   }       
      //  }).then(response => console.log(response));
        }).then((response) => this.setState({ racks: response.data }));
}

stockItemId 中的

值作为静态值传递,并且结果正确显示在控制台中.如何从1st-api请求动态获取stockItemId的值?

Value in stockItemId is passed as static value and result displayed in console correctly. How can get stockItemId's value dynamically from 1st-api request?

下面是直接在api中传递stockItemId并从第一个api获取数据的数据结果屏幕截图.

Below is data result screenShots of passing stockItemId directly in api and getting from 1st api.

  • Getting from 1st api: stockItemId: stockItems.data.StockItemId : http://prntscr.com/i7k0j7
  • Directly passing value of stockItemId screenshot- stockItemId: '2f80b45c-85ff-449b-9ad6-ffcc4bb640dd' http://prntscr.com/i7jyq7

推荐答案

第一问题 从不 使用componentWillMount组件生命周期方法 设置组件状态或调用任何api请求 出于这些目的,请使用componentDidMount来阅读有关该用途的生命周期的更多信息,请阅读

First thing never use componentWillMount component life cycle method to set the component state or call any api request for these purpose use componentDidMount for more reading which life cycle use for which purpose read this article and Secondly just add the second api request inside the first api request response with different name response name as given below:

componentDidMount() {
    axios.post('https://APISITE/api/Auth/AuthorizeByApplication?
        applicationId='app id'&applicationSecret='app secret'&token='app token'')
    .then((responseFirst) => { 
        axios.get('https://APISITE//api/Stock/GetStockItems', { 
            params: {
                keyWord: 5055967419551,
                locationId: '',
                entriesPerPage: 100000,
                pageNumber: 1,
                excludeComposites: true,
            },
            headers: { 
                Authorization: '0f32ae0d-c4e0-4aca-8367-0af88213d668' 
            }
        }).then((responseSecond) => this.setState({ products: responseSecond.data }));

            axios.get('https://APISITE//api/Stock/GetStockLevel', {  
                params: { 
                    stockItemId: responseFirst.data.stockItemId,
                },
                headers: { 
                    Authorization: '0f32ae0d-c4e0-4aca-8367-0af88213d668'
                }       
            }).then((responseThird) => this.setState({ racks: responseThird.data }));
    });
}

如果您正在使用redux,请阅读 redux文档以处理异步类型的操作以及处理方法.

if you are using redux then read redux documentation to handle async type actions and how to handle it.

这篇关于参数从一个api传递到react-native的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆