InfiniteLoader和react-redux

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

来自react-virtualised的组件InfiniteLoader需要作为属性loadMoreRows传递的函数具有类似{ startIndex: number, stopIndex: number }): Promise签名{ startIndex: number, stopIndex: number }): Promise 。 我在我的项目中使用redux,所以loadMoreRows是这样的redux动作创建者:

const fetchEntities(start, stop) {
  return fetch(`${myUrl}&start=${start}?stop=${stop}`)
}
const loadMoreRows = ({ startIndex, stopIndex }) => {
  return (dispatch, getState) => {
    return function(dispatch) {
      return fetchEntities(startIndex, stopIndex).then(
        items => dispatch(simpleAction(items)),
        error => console.log(error)
      )
    }
  }
}

之后,使用react-redux中的connect函数将此操作连接到包含InfiniteLoader的react组件。

所以我不确定,我怎样才能满足签名要求,因为redux动作创建者不会返回任何值/

react-redux react-virtualized
1个回答
2
投票

eyeinthebrick是对的。 承诺不是必需的返回值。

当你“连接”一个Redux动作创建者时,调用它(调度它)实际上会返回一个Promise。 所以例如我认为你可以做更像这样的事情......

function fetchEntities (start, stop) {
  return fetch(`${myUrl}&start=${start}?stop=${stop}`)
}

const loadMoreRows = ({ startIndex, stopIndex }) => {
  return async (dispatch, getState) => {
    try {
      const items = await fetchEntities(startIndex, stopIndex)
      await dispatch(simpleAction(items))
    } catch (error) {
      console.log(error)
    }
  }
}

此时, InfiniteLoader可以等待返回的Redux承诺。

© www.soinside.com 2019 - 2024. All rights reserved.