【问题标题】:Staying DRY with Async Calls in React Redux Application With Redux Thunk使用 Redux Thunk 在 React Redux 应用程序中使用异步调用保持 DRY
【发布时间】:2019-06-07 04:48:22
【问题描述】:

问题

我目前遇到了一个问题,我想如何执行可由多个触发的 AJAX 请求 页面上的不同 UI 元素。 AJAX 请求总是发送到同一个端点,并且总是发送相同的属性 从 redux 存储到端点(尽管属性值可能会因用户交互而改变)。

我完全意识到我目前的实现很糟糕。

破败

为了画得更清楚,我正在构建一个搜索页面,其中几个 UI 元素可以触发一个新的搜索被激活。 有一个端点,我们称之为“/api/search/”,它需要一个查询字符串,其中包含从 Redux 存储中提取的数据。它看起来像这样:

term=some%20string&categories=243,2968,292&tags=11,25,99&articleType=All

当 UI 元素需要触发对商店的同步更新,但还需要触发执行搜索的 Thunk 时,我会感到困惑。这是我的顶级组件,我将“executeSearch”Thunk 函数传递给所有需要触发搜索的子组件。我最初的想法是,我可以使用一个 thunk 来处理所有需要执行搜索的交互,而不是为每个交互编写一个单独的 thunk。

附:如果对您没有意义,请不要过度分析下面的代码。如果您浏览以下部分,然后阅读“三个场景”部分,这可能有助于您更好地理解一切是如何工作的。图片也包含在该部分中。

class App extends Component {
  executeSearch = () => {
    this.props.executeSearch(this.props.store); // This is my Thunk
  };

  render() {
    const { updateSearchTerm, clearAll, dropdownActive, dropdownType } = this.props;

    return (
      <section className="standard-content">
        <div className="inner-container narrow">
          <div className="centered">
            <h1>Search</h1>
            <h2 className="alt">Search our extensive database of research articles.</h2>
          </div>

          <SearchTerm initSearch={this.executeSearch} updateSearchTerm={updateSearchTerm} />
          <ExtraOptions clearAll={clearAll} />
          <Filters executeSearch={this.executeSearch} />
        </div>

        {dropdownActive ? (
          dropdownType === 'categories' ? (
            <CategoryDropdown executeSearch={this.executeSearch} />
          ) : (
            <TagDropdown executeSearch={this.executeSearch} />
          )
        ) : null}

        <SearchResults />
      </section>
    );
  }
}

const mapStateToProps = state => {
  return {
    store: state,
    dropdownActive: state.dropdownActive,
    dropdownType: state.dropdownType
  };
};

executeSearch 函数从存储中获取所有值,但仅使用我在本期开头概述的值。如果有帮助,本文底部有整个 redux 存储的代码示例。无论如何,这就是 Thunk 的样子:

export const executeSearch = criteria => {
  const searchQueryUrl = craftSearchQueryUrl(criteria);

  // if (term === '' && !selectedCategories && !selectedTags && articleType === 'All') {
  //   return { type: ABORT_SEARCH };
  // }

  return async dispatch => {
    dispatch({ type: FETCH_SEARCH_RESULTS });

    try {
      const res = await axios.post(`${window.siteUrl}api/search`, searchQueryUrl);
      dispatch({ type: FETCH_SEARCH_RESULTS_SUCCESS, searchResults: res.data });
    } catch (err) {
      dispatch({ type: FETCH_SEARCH_RESULTS_FAILED });
    }
  };
};

// Helper function to craft a proper search query string
const craftSearchQueryUrl = criteria => {
  const { term, articleType, selectedCategories, selectedTags } = criteria;
  let categoriesString = selectedCategories.join(',');
  let tagsString = selectedTags.join(',');

  return `term=${term}&articleType=${articleType}&categories=${categoriesString}&tags=${tagsString}&offset=${offset}`;
};

请记住,这里的“条件”参数是我在 App.js 中作为参数传递的整个商店对象。你会看到我只使用了 craftSearchQueryUrl 函数中我需要的属性。

三种场景

我附上了一张截图(标有字母),我希望在其中解释哪些地方可行,哪些地方不可行。

A.) 用户应该能够填写此文本字段,并且当他们按下放大镜时,它应该会触发 Thunk。这很好用,因为文本字段中的值在每次击键时都会在存储中更新,这意味着存储中的值在用户甚至有机会按下放大镜之前始终是最新的。

B.) 默认情况下,在初始页面加载时选中“全部”复选框。如果用户单击旁边列出的其他复选框之一,则应立即启动搜索。这是我的问题开始出现的地方。这是我目前拥有的代码:

export default ({ handleCheckboxChange, articleType, executeSearch }) => (
  <div style={{ marginTop: '15px', marginBottom: '20px' }}>
    <span className="search-title">Type: </span>

    {articleTypes.map(type => (
      <Checkbox
        key={type}
        type={type}
        handleCheckboxChange={() => {
          handleCheckboxChange('articleType', { type });
          executeSearch();
        }}
        isChecked={type === articleType}
      />
    ))}
  </div>
);

当复选框更改时,它会更新商店中的 articleType 值(通过 handleCheckboxChange),然后说执行从 App.js 传递下来的搜索功能。但是,更新后的 articleValue 类型并不是更新后的类型,因为我相信在商店有机会更新此值之前调用了搜索函数。

C.) 来自 B 的同样问题也出现在这里。当您单击“优化依据”部分中的一个按钮(类别或标签)时,此下拉菜单会出现多个可供选择的复选框。我实际上存储了哪些复选框在本地状态下被选中/取消选中,直到用户单击保存按钮。按下保存按钮后,应在商店中更新新选中/未选中的复选框值,然后应通过从 App.js 传递的 Thunk 启动新的搜索。

export default ({ children, toggleDropdown, handleCheckboxChange, refineBy, executeSearch }) => {
  const handleSave = () => {
    handleCheckboxChange(refineBy.classification, refineBy);
    toggleDropdown('close');
    executeSearch(); // None of the checkbox values that were changed are reflected when the search executes
  };

  return (
    <div className="faux-dropdown">
      <button className="close-dropdown" onClick={() => toggleDropdown('close')}>
        <span>X</span>
      </button>
      <div className="checks">
        <div className="row">{children}</div>
      </div>

      <div className="filter-selection">
        <button className="refine-save" onClick={handleSave}>
          Save
        </button>
      </div>
    </div>
  );
};

重要的是要注意,执行搜索时使用的值不是 B 和 C 的更新值,但它们实际上在存储中正确更新。

其他解决方案

我的另一个想法是创建一个 redux 中间件,但老实说,在尝试其他任何事情之前,我真的可以在这方面寻求一些专家的帮助。理想情况下,公认的解决方案的解释应该是彻底的, 并包含一个在处理 Redux 应用程序时考虑最佳架构实践的解决方案。也许我只是在这里做一些根本错误的事情。

杂项

如果有帮助,这就是我的完整商店(处于初始状态)的样子:

const initialState = {
  term: '',
  articleType: 'All',
  totalJournalArticles: 0,
  categories: [],
  selectedCategories: [],
  tags: [],
  selectedTags: [],
  searchResults: [],
  offset: 0,
  dropdownActive: false,
  dropdownType: '',
  isFetching: false
};

【问题讨论】:

  • 这应该是一个悬赏问题
  • 它让人们更有动力回答这个问题,因为如果他们回答正确,我必须将我的一些声誉积分赠送给他们。
  • 我也很难马上接受这个问题。我认为这需要相当多的时间投入。是的,您最初可能不会得到合适的答案。至于 redux thunk,它主要适用于简单的场景。自定义中间件很可能会更好地工作。考虑检查现有的,如 Redux Saga 或 Redux Logic。
  • 我们拭目以待。也许有人可以弄清楚。如果需要,我绝对可以澄清,如果有不清楚的地方更新答案。实际上,我认为对于在该领域具有丰富经验的人来说,这并不是一个很难的问题。也许我错了。
  • @estus 我也遇到过这种情况

标签: javascript reactjs redux redux-thunk


【解决方案1】:

使用 Ryan Cogswell 解决方案效果非常好。我能够删除所有让事情变得混乱的代码,并且不再需要担心将一些状态从存储传递给我的异步操作创建者,因为我现在使用的是 Redux Saga。

我重构的 App.js 现在看起来像:

class App extends Component {
  render() {
    const {
      updateSearchTerm,
      clearAll,
      dropdownActive,
      dropdownType,
      fetchSearchResults
    } = this.props;

    return (
      <section className="standard-content">
        <div className="inner-container narrow">
          <div className="centered">
            <h1>Search</h1>
            <h2 className="alt">Search our extensive database of research articles.</h2>
          </div>

          <SearchTerm fetchSearchResults={fetchSearchResults} updateSearchTerm={updateSearchTerm} />
          <ExtraOptions clearAll={clearAll} />
          <Filters />
        </div>

        {dropdownActive ? (
          dropdownType === 'categories' ? (
            <CategoryDropdown />
          ) : (
            <TagDropdown />
          )
        ) : null}

        <SearchResults />
      </section>
    );
  }
}

我需要做的就是将 fetchSearchResults 操作创建器导入我需要的任何 redux 连接组件中,并确保它被执行。例如:

export default ({ handleCheckboxChange, articleType, fetchSearchResults }) => (
  <div style={{ marginTop: '15px', marginBottom: '20px' }}>
    <span className="search-title">Type: </span>

    {articleTypes.map(type => (
      <Checkbox
        key={type}
        type={type}
        handleCheckboxChange={() => {
          handleCheckboxChange('articleType', { type });
          fetchSearchResults();
        }}
        isChecked={type === articleType}
      />
    ))}
  </div>
);

这是动作创建者现在的样子...

export const fetchSearchResults = () => ({ type: FETCH_SEARCH_RESULTS });

这使您的动作创建者保持纯粹且超级简单的推理。每当在我的 UI 的任何部分调度此操作时,我的 saga 都会选择此操作,并使用商店的最新版本执行搜索。

这是让它发挥作用的传奇:

import { select, takeLatest, call, put } from 'redux-saga/effects';
import { getSearchCriteria } from '../selectors';
import { Api, craftSearchQueryUrl } from '../utils';
import {
  FETCH_SEARCH_RESULTS,
  FETCH_SEARCH_RESULTS_SUCCESS,
  FETCH_SEARCH_RESULTS_FAILED
} from '../actions';

function* fetchSearchResults() {
  const criteria = yield select(getSearchCriteria);

  try {
    const { data } = yield call(Api.get, craftSearchQueryUrl(criteria));
    yield put({ type: FETCH_SEARCH_RESULTS_SUCCESS, searchResults: data });
  } catch (err) {
    yield put({ type: FETCH_SEARCH_RESULTS_FAILED, error: err });
  }
}

export default function* searchResults() {
  yield takeLatest(FETCH_SEARCH_RESULTS, fetchSearchResults);
}

【讨论】:

    【解决方案2】:

    App 中的以下块是问题的症结所在:

      executeSearch = () => {
        this.props.executeSearch(this.props.store); // This is my Thunk
      };
    

    这个executeSearch 方法在渲染App 时将store 烘焙到其中。

    当你这样做时:

      handleCheckboxChange('articleType', { type });
      executeSearch();
    

    当您到达executeSearch 时,您的 Redux 存储将同步更新,但这会产生一个新的 store 对象,这将导致 App 被重新渲染,但 不会 影响executeSearch 正在使用的store 对象。

    正如其他人在 cmets 中指出的那样,我认为最直接的处理方法是使用中间件,该中间件提供了一种机制来执行副作用,以在商店更新后响应 redux 操作。为此,我个人会推荐 Redux Saga,但我知道还有其他选择。在这种情况下,您将有一个 saga 来监视应该触发搜索的任何操作,然后 saga 是唯一调用 executeSearch 的东西,并且 saga 将能够将更新后的 store 传递给 executeSearch .

    【讨论】:

    • 你是对的。 Redux Saga 是要走的路。我已经在 React Native 应用程序中使用过它,并且可能应该使用它。谢谢。
    • 我已经发布了我所做的额外更新,以使其作为答案发挥作用。我不会更改选择的答案,但希望这可能对将来的某人有所帮助。
    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2018-09-10
    • 2018-01-31
    • 2020-05-16
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多