Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InfiniteLoader and react-redux

Component InfiniteLoader from react-virtualised requires function passed as property loadMoreRows to have signature like { startIndex: number, stopIndex: number }): Promise. I'm using redux in my project, so loadMoreRows is a redux action creator like this:

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)
      )
    }
  }
}

after that, this action is connected to react component containing InfiniteLoader using connect function from react-redux.

So I'm not sure, how can I satisfy signature requirement, as redux action creators don't return any value/

like image 718
eyeinthebrick Avatar asked Aug 04 '16 09:08

eyeinthebrick


1 Answers

eyeinthebrick is correct. A Promise is not a required return value.

When you "connect" a Redux action-creator, invoking it (dispatching it) actually returns a Promise. So for example I think you could do something more like this...

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)
    }
  }
}

At which point InfiniteLoader can just await the returned Redux promise.

like image 93
bvaughn Avatar answered Sep 28 '22 04:09

bvaughn