qweweretrt515

react-redux 能让其他组件对某个组件的点击事件作出反应吗?好像 redux 只能用来管理变量哦

  •  
  •   qweweretrt515 · Jun 1, 2017 · 2340 views
    This topic created in 3357 days ago, the information mentioned may be changed or developed.

    有个组件 a, 它有子组件,也有父组件,还有兄弟组件

    我希望组件 a 的按钮点击事件,能被其他的组件捕捉到, 用 redux 能实现吗 ,

    最近学 redux 好像都是用来改变某个变量的值的,不能 响应事件哦

    4 replies    2017-06-14 17:10:30 +08:00
    Exin
        1
    Exin  
       Jun 2, 2017 via iPhone   ❤️ 1
    在事件回调函数 dispatch 一个 action 就可以吧
    只要你的“其他组件”也在 redux 的管理下
    otakustay
        2
    otakustay  
       Jun 2, 2017   ❤️ 1
    你绑个点击然后改一下某个变量的值就好了呗
    Yuigahama
        3
    Yuigahama  
       Jun 2, 2017   ❤️ 1
    ```
    const listeners = currentListeners = nextListeners
    for (let i = 0; i < listeners.length; i++) {
    const listener = listeners[i]
    listener()
    }
    ```
    这是 `redux` 的 `dispatch` 里的代码,你每次 `dispatch` 都会调用 `listener`,我没看过 `react-redux` 的源码,不过应该是根据这个来更新进行更新。
    这样你只要在按钮的点击事件绑定一个 `dispatch`,就能触发 `react-redux` 去更新组件
    剩下的就是 `react` 自身的组件检测,就像 @otakustay 说的,redux 传组件的 props 里用一个不同的值来表示变更就好了(按你的需求,那个值就就可以是事件的相关信息
    horizon
        4
    horizon  
       Jun 14, 2017
    react 是数据驱动的。。
    你这个功能不需要 redux。 搜索 “ lift state up ”

    但是如果你已经用了 redux 的话,不是通过被其他组件捕捉到事件实现效果的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3125 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:38 · PVG 21:38 · LAX 06:38 · JFK 09:38
    ♥ Do have faith in what you're doing.