zqlcrow
V2EX  ›  React

初学 redux,目录不知道怎么建

  •  
  •   zqlcrow · Apr 18, 2023 · 3832 views
    This topic created in 1301 days ago, the information mentioned may be changed or developed.

    我看教程,是:
    /store/index.ts
    /store/modules/a.ts
    /store/modules/b.ts

    a.ts 和 b.ts 中,分别有各自的 action,reducer 。 这样的目录结构。


    我以前做 App 的习惯相当于是:
    /store/index.ts
    /modules/A/a.ts 、A.modules.scss 、A.tsx...
    /modules/B/b.ts 、B.modules.scss 、B.tsx...

    喜欢把和 A 有关的东西,尽量都塞在 A 里面。
    不过毕竟其实 App 没有这个 /store/index.ts 。


    前者的优势在?
    单纯的习惯问题?

    Supplement 1  ·  Apr 19, 2023
    在各位的一致反对下,把教程中的 redux 换成了 zustand 。

    目前改成使用 zustand 。
    14 replies  •  2023-05-04 20:54:35 +08:00
    zqlcrow
    zqlcrow
    OP
    Apr 18, 2023
    1
    举个例子:
    购物车。



    1 、购物车相关的代码,分别分散在:
    actions/cartActions.js
    components/Cart.js
    reducers/cartReducer.js


    2 、我多年 app 的习惯是,购物车相关的代码,尽量全部在 cart 模块(目录)中。
    cart/cartActons.js
    cart/cartReducers.js
    cart/Cart.js
    sweetcola
    sweetcola
    Apr 18, 2023
    2
    现在官方是推荐使用 [RTK]( https://redux-toolkit.js.org/usage/usage-guide),React-Redux 已经不是推荐使用的方式了。

    目录只要能通过名称看得懂这个目录里装着什么就可以了。

    我的话是直接
    /redux/store.ts
    /redux/slice/*Slice.ts
    shaozelin030405
    shaozelin030405
    Apr 18, 2023
    3
    直接 reducer ,不需要 action 。之后引入 redux-saga ,可以 reducer 一起放 effect
    hervey0424
    hervey0424
    Apr 18, 2023
    4
    直接用 mobx 吧, 对 ts 支持更好
    wu67
    wu67
    Apr 18, 2023
    5
    建议 recoil. 概念少, 使用简单. 我主用 vue 的, 去年随便看看文档就能开写了.

    https://recoiljs.org/zh-hans/docs/introduction/core-concepts
    zhwithsweet
    zhwithsweet
    Apr 18, 2023 via iPhone
    6
    jotai zustand. Redux 千万别用
    sl0000
    sl0000
    Apr 18, 2023
    7
    我看 redux 都头疼
    ruoxie
    ruoxie
    Apr 18, 2023 via iPhone
    8
    求求别再用 redux 了,为什么想不开
    hgaogao
    hgaogao
    Apr 18, 2023 via iPhone
    9
    @zhwithsweet jotai 都不行吗?听说挺简洁的呀?有什么说法吗?
    zhwithsweet
    zhwithsweet
    Apr 18, 2023
    10
    @hgaogao 我的断句问题;用 jotai 或者 zustand; 千万别用 redux (redux 极其难用
    linkopeneyes
    linkopeneyes
    Apr 18, 2023
    11
    没什么必要用 redux 除非是老项目维护,其实如果不在乎性能直接 content 就好了,什么库都还是要学概念
    zhoupeng199
    zhoupeng199
    Apr 19, 2023
    12
    为啥要用 redux 这么难用的东西,建议 Mobx ,天生面向对象编程
    karott7
    karott7
    Apr 19, 2023
    13
    看到这些状态库就头大,一堆概念,我现在都是用 React.createContext + immer 自己封装下,全局一个 Context ,业务使用独自的 Context ;
    个人看法,我认为自从 react-router-dom 带来 data-router 功能之后,loader + useLoaderData + useRouteLoaderData 的组合可以直接宣告状态库死亡了,而且 react@18 不是也增加了啥状态管理功能么
    humbass
    humbass
    May 4, 2023
    14
    我是 6 年的 vuer ,第一次尝试用 react 做完一个项目,记得刚开始配置 redux ,花了 1 天基本的 demo 都没跑通,Jotai 基本上 几分钟之内就可以上手。

    我觉得阻挡大部分 vue 用户进入 react 的都是这些基础的东西没完善,或者说脚手架做的不好。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1098 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 23:04 · PVG 07:04 · LAX 15:04 · JFK 18:04
    ♥ Do have faith in what you're doing.