第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何從 React Native 中的輔助函數(shù)分派動作

我如何從 React Native 中的輔助函數(shù)分派動作

qq_笑_17 2021-06-30 16:01:18
我有一個helper.js文件,其中包含所有輔助函數(shù),包括帶有 axios 的 HTTP 請求處理程序。這是我的helper.js文件代碼:const HTTPRequest = (path, body, method = 'POST', authorizedToken = null) => {    return new Promise((resolve, reject) => {        let headers = {            'Content-type': 'multipart/form-data',        };        // Set authorization token        if (authorizedToken) {            headers['Authorization'] = "JWT " + authorizedToken;  // Here i want to get user token from the redux store not though passing the token        }        const fulHeaderBody = {            method,            headers,            timeout: 20,            url: path        };axios(fulHeaderBody).then(response => {            if (response.success) {                resolve(response);            } else {                // Show toast about the error                Toast.show({                    text: response.message ? response.message : 'Something went wrong.',                    buttonText: 'Okay',                    type: 'danger'                });                resolve(response);            }        }).catch(error => {            if (error.response) {               if(error.response.status === 401){                    // I WANT TO DISPATCH AN ACTION HERE TO LOGOUT CLEAR ALL STORAGE DATA IN REDUX AND CHANGE THE STATE TO INITIAL               }else{                    console.log('Error', error.message);               }            }  else {                // Something happened in setting up the request that triggered an Error                console.log('Error', error.message);            }            console.log(error.config);            reject(error);        })    });};export default HTTPRequest;現(xiàn)在,我面臨的問題是,我如何從這個輔助函數(shù)中分派一個動作,或者我如何user token從 redux 存儲中獲取。并helper.js像這樣調(diào)用它內(nèi)部HTTPRequest功能helloWorld();我只能看到日志Hello world has been dispatched,但我沒有看到調(diào)度員的任何日志。誰能告訴我如何處理這個問題。?
查看完整描述

2 回答

?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

我假設(shè)您已經(jīng)安裝了 redux-thunk。


首先,您需要修改helper.js 中的HTTPRequest函數(shù)。添加兩個參數(shù)給它調(diào)度,如下所示


const HTTPRequest = (path, body, method = 'POST',

                      authorizedToken = null,dispatch,actionCreator) => {

然后在您的 axios 調(diào)用成功后,您可以添加以下行


 dispatch(actionCreator.success(response))

同樣對于失敗,您可以添加如下


 dispatch(actionCreator.failure(error.message))

在 action.js 文件中創(chuàng)建一個動作作為


export const helperAction=(path, body, method = 'POST',

                      authorizedToken = null) =>{

var actionCreator = {}

actionCreator.success = helloWorld

actionCreator.failure = failureFunction //your failure action


return dispatch => {

  HTTPRequest(path, body, method = 'POST',

                      authorizedToken = null,dispatch, actionCreator)

 }

}

創(chuàng)建 action creator 并將其作為參數(shù)與 dispatch 一起傳遞給 helper.js 文件中可用的 HTTPREQUEST 實用程序。現(xiàn)在基于成功和失敗的響應(yīng),它正在啟動操作。使用該操作,您可以將響應(yīng)存儲在 redux 存儲中,然后您可以在您的組件中使用它。


更新了下面的答案以解決確切的問題。否則我會推薦上述解決方案。試試這個


import store from './redux/store.js';

Const func=()=> {}


store.subscribe(func)


查看完整回答
反對 回復(fù) 2021-07-08
?
牛魔王的故事

TA貢獻1830條經(jīng)驗 獲得超3個贊

通過@Avin Kavish 在評論中分享的鏈接,這是我的解決方案。


在helper.js我已經(jīng)導(dǎo)入了商店,現(xiàn)在輔助函數(shù)看起來像這樣


import {store} from "../redux/ConfigureStore"; //I have imported my store


const HTTPRequest = (path, body, method = 'POST', authorizedToken = false) => {

 let getInitialStates = store.dispatch(helloWorld()); //This to dispatch an action

   <ALL OTHER CODES AS ABOVE HERE>

}

這是因為 store 是一個對象,它幾乎沒有方法,包括調(diào)度這里提到的動作。


查看完整回答
反對 回復(fù) 2021-07-08
  • 2 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號