首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react-hooks中使用中间件

在react-hooks中使用中间件
EN

Stack Overflow用户
提问于 2019-04-05 18:28:41
回答 1查看 1.4K关注 0票数 0

因此,我在我的react原生应用程序中使用redux进行状态管理。我期待着在我的应用程序中使用钩子。我的存储有多个单个reducer的实例,因此将操作分派到单个reducer是一件很痛苦的事情。

在这种情况下,钩子似乎提供了一个非常好的解决方案。我们正在使用中间件来捕获特定的操作并执行副作用。我希望为钩子实现一个类似中间件的功能,其中分配给钩子的操作也可以在redux中间件中捕获。

任何关于如何实现这些功能的建议都将受到高度赞赏。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-04-07 00:33:49

你是否听说过或尝试过redux-saga用于中间件异步的事情,如数据获取和访问浏览器缓存等不纯的事情更容易管理,更有效的执行,更容易测试,更好地处理故障。

有关更完整的文档,请参阅here

我给你举了一个简单的例子,说明如何使用redux saga作为副作用。

代码语言:javascript
复制
import api from '../api/apidetailproduct'; //if using api import it
import {
    takeLatest, //Spawns a saga on each action dispatched to the Store that matches pattern
    select, //instructs the middleware to invoke the provided selector on the current Store's state
    call, //for calling api
    put //instructs the middleware to dispatch an action to the Store. 
} from 'redux-saga/effects';
import {
    REQUEST_PRODUCT, 
} from '../actions/constants';
import {
    responseProduct,
} from '../actions/product';

export function* detailEffect(){
    const {loginreducer} = yield select() //first select your reducer where you store
    try {
        const {data} = yield call(api, loginreducer.token )//calling the api and then send the variable
        yield put(responseProduct(data))//put the data
    } catch (error) {
        console.log("error", error);
    }
}

export const productsagas = [takeLatest(REQUEST_PRODUCT, detailEffect)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55533417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档