根据NgRx文件减少器和选择器是纯功能。
“教育者是纯功能,因为他们为特定投入生产相同产出”。
“由于甄选者是纯粹的职能,最后的结果可以在提出论据的同时放弃你的甄选职能。
考虑以下简单例子
export const AppState = {
dataArray: []
}
// A Simple action that adds to the dataArray (created by Action creator)
export const addAction = createAction( Add to dataArray , props<{ data:number }>())
// Initial state of dataArray is an empty array
const initialDataArray = [];
// A simple reducer to get new state (created by Reducer creator)
// The new data element is simply added to the previous dataArray and a new Array is returned
export const dataArrayReducer = createReducer(initialDataArray,
on(addAction, (state, newData) => ([...state.dataArray, newData])))
// A selector to get the dataArray
export const selectDataArray = (state: AppState) => state.dataArray;
// Suppose we dispatch actions to the store
store.dispatch(addAction(1)); // Reducer will return state : { dataArray : [1] }
store.dispatch(addAction(1)); // Reducer will return state : { dataArray [1, 1] }
// Different states returned even though the input was the same Action with same props
const dataArray$ = store.pipe(select(selectDataArray)); // [1, 1]
store.dispatch(addAction(1));
const dataArray2$ = store.pipe(select(selectDataArray)); // [1, 1, 1]
// We get different values from the same selector.
So as we can see we get different values from the Reducer and Selector even when we provide the exact same arguments. So how can they be considered Pure functions?
我失踪了吗? 我是否认为保有权职能的定义是错误的?