简体   繁体   English

为什么我们使用()=> []而不是[]?

[英]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.

相关问题 为什么我们使用promise而不是if / then语句? - Why do we use promises instead of if/then statements? 为什么我们使用 JWT 而不是 firebase - Why do we use JWT instead of firebase 为什么我们使用[Symbol.iterator]而不是`for`循环? - Why do we use [Symbol.iterator] instead of a `for` loop? 为什么我们在 Javascript 中使用术语阴影而不是覆盖? - Why do we use the term shadow instead of override in Javascript? 当我们可以使用 delete 运算符时,为什么我们需要在 Lodash 中使用 pickBy? - Why do we needed pickBy in Lodash when We can use delete operator instead? 为什么我们使用双斜杠而不是http - why we use double slash instead http 为什么我们在前端框架而不是CommonJS中使用ES6模块进行导入 - Why do we use ES6 modules for import in frontend frameworks instead of CommonJS 在JavaScript中,为什么我们使用el = document.createElement()来创建一个Element对象而不是el = new Element(); - In JavaScript, why do we use el = document.createElement() to create an Element object instead of el = new Element(); 为什么我们在 React 中使用路由而不是开关? 而我们的工作是由开关完成的 - Why do we use Routes instead of Switches in React? Whereas our work is done by the switch 为什么不只使用object(map)来表示邻接列表边? 如果改为使用数组,则需要执行额外的线性查找操作,不是吗? - Why not just use object(map) for adjacency list edges representation? If instead we use array, we need to do extra linear find operation, isn't it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM