检查动作创造者内部
checks inside action creators
我正在创建简单的应用程序,它向服务器发出 GET 请求,然后准备收到的数据并创建图表。有几个问题:
我应该在哪里放置负责检查和准备原始数据的代码。目前我在我的 action creators 中有它,但也许它需要在组件本身中?
我需要检查并比较准备好的数据和已经用于图表的数据,如果相同或无效则不调用重新渲染。我应该把这张支票放在哪里?现在我想把它也放在 action creators 中。但是为此我需要使用 getState()
来访问状态,看起来不对。
Action creators 似乎对我来说是所有这些检查的正确位置,因为如果数据无效,我可以简单地不使用它更新我的状态,(例如,不派遣某些 action creator)或者也许我必须用新数据更新状态,尽管它无效?
鉴于这些动作创建者,描述检查的最佳位置是什么?:
export function fetchPopulations(term = "") {
return function (dispatch) {
dispatch(fetchingPopulations())
term=toTitleCase(term)
return fetch(`${API_URL}${term.replace(/\s/g, '%20')}`)
.then(response => response.json())
.then(json => dispatch(requestPopulations(json)))
}
}
export function requestPopulations(data = []) {
return {
type: REQUEST_POPULATIONS,
payload: data,
}
}
export function fetchingPopulations() {
return {
type: FETCHING_POPULATIONS
}
}
我会说你做得对。
在您的示例中,requestPopulations
和 fetchingPopulations
是真正的动作创建者,而 fetchPopulations
是一个组合函数(是的,组合函数是为了胜利!)。
Where should I place code responsible for checking and preparing raw
data. Currently I have it in my action creators, but maybe it needs
to be in the component itself?
组件不是放置我们应用程序业务逻辑的地方。组件应该只代表我们 MVC 中的 View。没有 API 调用,没有业务逻辑,只有道具和状态。
I need to check and compare prepared data with the data which is
already used for the chart, and do not call re-render if it's the same
or not valid. Where should I put this check? For now I think to place
it inside action creators too. But for that I need to use getState()
for accessing the state, doesn't look right.
创建模块化函数(它在代码维护和重用方面确实很出色)来执行这些检查,将它们与您的实际操作创建者一起组合在另一个函数中,并且您可以仅在需要时分派。可以在组件生命周期钩子 shouldComponentUpdate(nextProps, nextState)
内部进行进一步的优化。另外我认为使用带有这样签名的方法绝对不是反模式:
export function myComposingFunction(params) {
return (dispatch, getState) => {
// ...
所以你可以使用getState()
.
Action creators seems right place for all these checks for me, because
if data is not valid, I can simply not update my state with it, (e.g.
do not dispatch certain action creator) Or maybe I have to update
state with new data despite it is not valid?
不,不要用无用的数据更新状态。如果你这样做,你将白白重新渲染整棵树。你说 "if data is not valid, I can simply not update my state with it, (e.g. do not dispatch certain action creator)"
完全正确
我正在创建简单的应用程序,它向服务器发出 GET 请求,然后准备收到的数据并创建图表。有几个问题:
我应该在哪里放置负责检查和准备原始数据的代码。目前我在我的 action creators 中有它,但也许它需要在组件本身中?
我需要检查并比较准备好的数据和已经用于图表的数据,如果相同或无效则不调用重新渲染。我应该把这张支票放在哪里?现在我想把它也放在 action creators 中。但是为此我需要使用
getState()
来访问状态,看起来不对。Action creators 似乎对我来说是所有这些检查的正确位置,因为如果数据无效,我可以简单地不使用它更新我的状态,(例如,不派遣某些 action creator)或者也许我必须用新数据更新状态,尽管它无效?
鉴于这些动作创建者,描述检查的最佳位置是什么?:
export function fetchPopulations(term = "") {
return function (dispatch) {
dispatch(fetchingPopulations())
term=toTitleCase(term)
return fetch(`${API_URL}${term.replace(/\s/g, '%20')}`)
.then(response => response.json())
.then(json => dispatch(requestPopulations(json)))
}
}
export function requestPopulations(data = []) {
return {
type: REQUEST_POPULATIONS,
payload: data,
}
}
export function fetchingPopulations() {
return {
type: FETCHING_POPULATIONS
}
}
我会说你做得对。
在您的示例中,requestPopulations
和 fetchingPopulations
是真正的动作创建者,而 fetchPopulations
是一个组合函数(是的,组合函数是为了胜利!)。
Where should I place code responsible for checking and preparing raw data. Currently I have it in my action creators, but maybe it needs to be in the component itself?
组件不是放置我们应用程序业务逻辑的地方。组件应该只代表我们 MVC 中的 View。没有 API 调用,没有业务逻辑,只有道具和状态。
I need to check and compare prepared data with the data which is already used for the chart, and do not call re-render if it's the same or not valid. Where should I put this check? For now I think to place it inside action creators too. But for that I need to use getState() for accessing the state, doesn't look right.
创建模块化函数(它在代码维护和重用方面确实很出色)来执行这些检查,将它们与您的实际操作创建者一起组合在另一个函数中,并且您可以仅在需要时分派。可以在组件生命周期钩子
shouldComponentUpdate(nextProps, nextState)
内部进行进一步的优化。另外我认为使用带有这样签名的方法绝对不是反模式:export function myComposingFunction(params) { return (dispatch, getState) => { // ...
所以你可以使用
getState()
.Action creators seems right place for all these checks for me, because if data is not valid, I can simply not update my state with it, (e.g. do not dispatch certain action creator) Or maybe I have to update state with new data despite it is not valid?
不,不要用无用的数据更新状态。如果你这样做,你将白白重新渲染整棵树。你说 "if data is not valid, I can simply not update my state with it, (e.g. do not dispatch certain action creator)"
完全正确