[英]Why do we use () => [] instead of []?
i have a short question about a reducer in React Native 我对React Native中的reducer有一个简短的问题
Why do I have to write the code like this: 为什么我必须这样编写代码:
import { combineReducers } from 'redux';
export default combineReducers({
reducerKey : () => []
});
instead of this: 代替这个:
import { combineReducers } from 'redux';
export default combineReducers({
reducerKey : []
});
Basically: Why does it have to be a function? 基本上:为什么必须要有一个功能? Thanks! 谢谢!
The job of the reducer is to apply some input to update the state. 减速器的工作是应用一些输入来更新状态。 The reducer returns the new state after an action is performed. 执行操作后,reducer返回新状态。 It works on an input and may return a different state on different input, hence it is a function. 它对输入起作用,并且在不同的输入上可能返回不同的状态,因此它是一个函数。
If you use the second approach, you generate a fixed value of state, which could be valid for some use cases. 如果使用第二种方法,则会生成状态的固定值,这对于某些用例可能是有效的。 But generally, you'd want to use a function. 但是通常,您会想要使用一个函数。
First, you need to know what are they? 首先,您需要知道它们是什么?
() => []
is an arrow function which returns an empty array but []
is just an empty array. () => []
是一个箭头函数 ,它返回一个空数组,但是[]
只是一个空数组。
See the doc's note : 请参阅文档说明 :
reducers (Object): An object whose values correspond to different reducing functions that need to be combined into one. reducers(对象):一个对象,其值对应于需要组合为一个的不同归约函数。
So, you'll need to pass a function in reducer. 因此,您需要在reducer中传递一个函数。 So you'll use () => []
instead of []
. 因此,您将使用() => []
而不是[]
。
I have never used such with combineReducers. 我从来没有用过CombineReducers这样的。 This is used to combine the different reducers like: 这用于组合不同的减速器,例如:
combineReducers({
reducer1,
reducer2
})
The preceding example is just an alias of: 前面的示例只是以下内容的别名:
combineReducers({
reducer1: reducer1,
reducer2: reducer2
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.