React Native Redux 状态更新保持导航调用

问题描述 投票:0回答:1

我的 React Native 应用程序中有两个页面:

  1. id
    title
    显示数据列表。通过按下 item,我们应该在 redux 中设置当前数据类型的 ID,并通过在包含所有
    state.current
    的 redux 切片内查找一个 item 来设置
    data
    。然后,我们导航到下一页。

示例:

    setCurrentData: (state, action) => {
      state.current = dataAdapter
        .getSelectors()
        .selectById(state, action.payload.id)
    },

选择器:

export const getCurrentCommunity = state => state.allData.current
  1. 第二页通过调用
    useSelector(getCurrentCommunity)
    获取数据并显示。

一切正常,我从 redux 正确传递/获取数据。问题是导航之前的计算非常慢。在 redux 中设置数据运行速度很快(

performance.now()
显示了良好的结果),但是状态更新之后的某些内容正在保留我的导航调用。

当然,我可以将数据作为参数传递,但这不是一个选项,因为稍后我想获取它而不将任何内容传递给没有路由的公共组件。

我该怎么做才能加快 redux 状态更新并使我的导航调用更快?

顺便说一句,如果我删除 redux 状态设置器,导航效果完美,延迟 70-80 毫秒(与 redux 的 2-3 秒相比)

react-native redux react-navigation
1个回答
0
投票

保存自己的

setCurrentData
操作 - 在商店中复制数据是一种不好的做法,像这样的派生数据应该只是选择器的一部分。

只需设置当前页面 id(如果有必要的话)

    setCurrentId: (state, action) => {
      state.currentId = action.payload.id
    },

const dataSelectors = dataAdapter.getSelectors(state => state.allData)
export const getCurrentCommunity = state => dataSelectors.selectById(state, state.allData.currentId)

甚至完全跳过该动作:

const dataSelectors = dataAdapter.getSelectors(state => state.allData)
export const getCurrentCommunity = (state, id) => dataSelectors.selectById(state, id)

并致电

useSelector(state => getCurrentCommunity(state, idFromNavigationProp)
© www.soinside.com 2019 - 2024. All rights reserved.