V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zqlcrow
V2EX  ›  React

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

  •  
  •   zqlcrow · 346 天前 · 2308 次点击
    这是一个创建于 346 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我看教程,是:
    /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 。


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

    第 1 条附言  ·  345 天前
    在各位的一致反对下,把教程中的 redux 换成了 zustand 。

    目前改成使用 zustand 。
    14 条回复    2023-05-04 20:54:35 +08:00
    zqlcrow
        1
    zqlcrow  
    OP
       346 天前
    举个例子:
    购物车。



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


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

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

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

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

    我觉得阻挡大部分 vue 用户进入 react 的都是这些基础的东西没完善,或者说脚手架做的不好。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2889 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:20 · PVG 22:20 · LAX 07:20 · JFK 10:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.