React Redux 与路由器位置、分页/排序和获取

React Redux with Router Location, Pagination / Sorting and Fetch

我正在编写一些具有以下功能的通用待办事项列表组件

现在我正在尝试为 "best" redux 操作序列找到一种模式,以使其正常工作。

我想要以下行为:

我当前代码的作用:

我正在 react-redux 的 mapStateToProps:

中创建道具 todospaginationsort
state => {
    todos: state.venues.page,
    pagination: parsePagination(state.router.location.search),
    sorting: parseSorting(state.router.location.search)
}

在组件中,我有生命周期和方法,例如:

componentDidMount() {
    // these props come from redux's "mapStateToProps"
    dipatch(executeFetch(this.props.pagination, this.props.sorting));
}

onSortingChange = (sorting) => {
    // this will use connected-react-router's "push"
    // to update the browser's URL
    dispatch(dispatchSetSortingQueryParam(sorting));
}

onPaginationChange = (pagination) => {
    // same as "onSortingChange"
    dispatch(setPaginationQueryParam(sorting));
}

现在打个大问号:

sortingpagination 发生变化时,我应该在哪里/如何 运行 executeFetch

您如何在您的应用程序中解决这个问题?

我认为这就是您描述的情况:

循环要求您进行相等性检查以仅在特定条件下更新 redux 状态。这是一个组件从 both url 状态和 redux 状态重新渲染而产生的问题。将该逻辑分成两个部分应该可行:

在实践中可能看起来像:

let App = () => (
  <Router>
    <>
      <Route component={class extends Component {
        componentDidUpdate() {
          let { pagination, sorting } = parse(window.location.search)
          dipatch(executeFetch(pagination, sorting));
        }
        render() { return null }
      }} />
      <Route path="/todo_list" component={props => (
        <div>
          {/* change url via link */}
          <Link to={...}>Change Page</Link>
          {/* change url programatically */}
          <input
            onChange={e => props.history.push(...)}
          />
        </div>
      )} />
    </>
  </Router>
)

todolist 没有调度操作来修改 url,而只是使用 <Link /> 组件或 history.push 添加分页和排序信息。