zqlcrow
V2EX  ›  React

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

  •  
  •   zqlcrow · Apr 18, 2023 · 3293 views
    This topic created in 1141 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
        1
    zqlcrow  
    OP
       Apr 18, 2023
    举个例子:
    购物车。



    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  
       Apr 18, 2023
    现在官方是推荐使用 [RTK]( https://redux-toolkit.js.org/usage/usage-guide),React-Redux 已经不是推荐使用的方式了。

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

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

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

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