gpt4 book ai didi

javascript - 如何在任何调度操作上运行 redux-saga 中间件?

转载 作者:行者123 更新时间:2023-12-02 22:17:07 24 4
gpt4 key购买 nike

我在“usersaga.js”中定义了以下 redux saga 中间件函数

import {call, put, takeEvery, takeLatest} from "redux-saga/effects";
import {REQUEST_API_DATA, receiveApiData} from '../store/actions';

import userServiceObject from '../services/user.service';


// Worker Saga function will be fired on USER_FETCH_ACTIONS

export function* getApiData(action){
console.log(action)
try{
// Do Api Call
const data = yield call(userServiceObject.getUsers);
console.log(data.data);
yield put(receiveApiData(data.data));
}
catch(error){
console.log("this is error part and executing");
console.log(error);
}
}

在“index.js”文件中,我使用“run”方法来运行上面的函数 getApiData

import React from 'react';
import ReactDOM from 'react-dom';
import {createStore, combineReducers, applyMiddleware } from 'redux';
import {Provider} from 'react-redux';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

import contactReducer from './store/reducers/userlist';

// For Creating Saga and connecting store to saga middleware
import createSagaMiddleware from 'redux-saga';
import {getApiData} from './sagas/usersaga';

const sagaMiddleware = createSagaMiddleware();

const rootReducer = combineReducers({
res: contactReducer
})

const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(getApiData);

ReactDOM.render(<Provider store={store}> <App /> </Provider>, document.getElementById('root'));



serviceWorker.unregister();

它成功地从 api 获取数据,并且我成功处理了由“receiveApiData”操作生成器函数生成的操作中的数据。
我想在由调度的函数“requestApiData”生成的操作上调用 getApiData 函数。操作名称为“{type: REQUEST_API_DATA}”
如何在分派(dispatch)的操作上运行我们的 getApiData。我的用户列表缩减程序文件如下所示

import * as actionTypes from "../actions";

const initialState = {
value: '',
results: []
};

const reducer = (state = initialState, action) => {
switch(action.type){
case actionTypes.STORE_CONTACTS: // This is for storing all contacts in the state here
console.log(action.values)
return {
...state,
results: action.values
}

case actionTypes.RECEIVE_API_DATA:
console.log(action);
return{
...state,
results: action.contacts
}
case actionTypes.DELETE_CONTACT:
return {
...state,
results: action.value
}
case actionTypes.ADD_CONTACT:
// perform add action to the database, update state and return All new data
return {
...state,

}
case actionTypes.EDIT_CONTACT:
return {
...state,
}
default:
return state;

}
};

export default reducer;

最佳答案

您需要使用实际上已经在 usersaga.js 中导入的 takeEvery 效果

所以在 usersaga.js 中添加这个传奇:

export function* requestWatcher(action){
yield takeEvery('REQUEST_API_DATA', getApiData);
}

在index.js中运行这个saga而不是直接运行getApiData:

sagaMiddleware.run(requestWatcher);

这是 redux-saga 的基本概念之一,我强烈建议阅读文档 https://redux-saga.js.org/docs/basics/UsingSagaHelpers.html

关于javascript - 如何在任何调度操作上运行 redux-saga 中间件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59353296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com