首页
/ Next-Redux-Saga 项目教程

Next-Redux-Saga 项目教程

2024-09-10 07:56:36作者:裘旻烁

1. 项目的目录结构及介绍

next-redux-saga/
├── examples/
│   └── basic/
│       ├── components/
│       ├── pages/
│       ├── reducers/
│       ├── sagas/
│       ├── store/
│       ├── .babelrc
│       ├── next.config.js
│       ├── package.json
│       └── yarn.lock
├── lib/
│   ├── index.js
│   └── withReduxSaga.js
├── .gitignore
├── LICENSE
├── package.json
├── README.md
└── yarn.lock

目录结构介绍

  • examples/: 包含项目的示例代码,其中 basic/ 是一个基本的示例项目。
    • components/: 存放 React 组件。
    • pages/: 存放 Next.js 的页面组件。
    • reducers/: 存放 Redux reducer 文件。
    • sagas/: 存放 Redux-Saga 的 saga 文件。
    • store/: 存放 Redux store 的配置文件。
    • .babelrc: Babel 配置文件。
    • next.config.js: Next.js 配置文件。
    • package.json: 示例项目的依赖管理文件。
    • yarn.lock: 锁定依赖版本的文件。
  • lib/: 存放项目的核心代码。
    • index.js: 项目的入口文件。
    • withReduxSaga.js: 用于集成 Redux-Saga 的高阶组件。
  • .gitignore: Git 忽略文件配置。
  • LICENSE: 项目许可证文件。
  • package.json: 项目的依赖管理文件。
  • README.md: 项目的说明文档。
  • yarn.lock: 锁定依赖版本的文件。

2. 项目的启动文件介绍

examples/basic/package.json

{
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  }
}
  • dev: 启动开发服务器。
  • build: 构建生产环境的应用。
  • start: 启动生产环境的应用。

examples/basic/next.config.js

module.exports = {
  // 配置项
};
  • next.config.js: Next.js 的配置文件,用于自定义 Next.js 的行为。

3. 项目的配置文件介绍

examples/basic/store/index.js

import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';
import { createWrapper } from 'next-redux-wrapper';
import rootReducer from '../reducers';
import rootSaga from '../sagas';

const makeStore = context => {
  const sagaMiddleware = createSagaMiddleware();
  const store = createStore(
    rootReducer,
    applyMiddleware(sagaMiddleware)
  );
  store.sagaTask = sagaMiddleware.run(rootSaga);
  return store;
};

const wrapper = createWrapper(makeStore);

export default wrapper;
  • makeStore: 创建 Redux store 的函数,使用 redux-saga 中间件。
  • wrapper: 使用 next-redux-wrapper 创建的 Redux store 包装器。

examples/basic/pages/_app.js

import React from 'react';
import App from 'next/app';
import withReduxSaga from 'next-redux-saga';
import wrapper from '../store';

class ExampleApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};
    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }
    return { pageProps };
  }

  render() {
    const { Component, pageProps } = this.props;
    return <Component {...pageProps} />;
  }
}

export default wrapper.withRedux(withReduxSaga(ExampleApp));
  • ExampleApp: 自定义的 Next.js 应用组件,使用 withReduxSagawrapper.withRedux 进行 Redux 和 Redux-Saga 的集成。

通过以上配置,next-redux-saga 项目可以成功集成 Redux 和 Redux-Saga,并在 Next.js 应用中使用。

登录后查看全文
热门项目推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
261
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
860
511
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
259
300
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
332
1.08 K
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
kernelkernel
deepin linux kernel
C
22
5