English 中文(简体)
NNgRx选择者和减少者是如何纯功能的?
原标题:How are NgRx Selectors and Reducers pure functions?

根据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?

我失踪了吗? 我是否认为保有权职能的定义是错误的?

问题回答

经过一点思考,我找到了我的回答。

值得记住的关键是,除其他论点外,减少者和选择者都把国家当作一个论点。 创造力和创造力,使国家必须履行的减员和选任人职能得以恢复。

因此,对于某个国家来说,同样的论点将永远收回同样的价值,这就是为什么这些价值观是纯功能。

The first time addAction is dispatched the state is { dataArray = [] } The second time addAction is dispatched the state is { dataArray = [1] }

因此,我们与削减者有着不同的价值观。

Similarly for the selector the first time the selector is called the state is { dataArray = [1, 1] } and the second time it is called the state is { dataArray = [1, 1, 1] }.

因此,选择人每次都回去不同的价值。

www.un.org/Depts/DGACM/index_spanish.htm 减少器:

  1. According to the NgRx documentation, reducers are indeed pure functions.
  2. A pure function produces the same output for a given input, without any side effects.
  3. When you invoke a reducer with the same arguments (including the current state), it consistently returns the same result. This property ensures predictability and consistency in state management.

// Initial state: an empty array
const initialDataArray: number[] = [];

// Reducer to add new data to the dataArray
const dataArrayReducer = createReducer(
  initialDataArray,
  on(addAction, (state, newData) => [...state, newData])
);




相关问题
maintain state with spring between requests

I am new to spring so sorry if this is a beginners question, but the manual is not clear (at least not for me) My question is: how do I share state between requests in spring? I can send data from ...

How to maintain state in silverlight?

When I resize my aspx page hosting the silverlight app, I lose state on all silverlight controls. How do I maintain state on silverlight controls?

State in ASP.net Master Page

We have a bug that we are trying to fix. It looks like one user can see another users data it they hit the same aspx page at the same time. I was wondering: If we store data in a property on the ...

A better design? Same object, different possible states

I have a very simple application which consists of an ASP.NET front end site, with a WCF Windows Service doing the heavy lifting back-end logic. The user has one simple page where he selects some ...

Session State with MVP and Application Controller patterns

I ve created an MVP (passive view) framework for development and decided to go for an Application Controller pattern to manage the navigation between views. This is targeted at WinForms, ASP.NET and ...

WCF, Custom Membership Provider and HttpContext

Ok, Im really going to show my stupidity regarding the ASP.NET security model here but here goes. I have a WCF web service and I have managed to hack my way around to having it pipe through my ...

EMAIL server FSP model

For my assignment I need to develop FSP model for email server and client. I manage to write simple model which describes one user, server and his mailbox, but I am having problems changing this ...

热门标签