我已经经历了几个JavaScript的概念,包括几个反应传达,有几个问题,我想知道,并希望得到你们的帮助。
假设我们有一个函数名和:
const sum = (a,b) => a + b问题1:我看到很多匿名函数被调用来调用另一个函数,我想知道为什么我们这样做而不是直接调用特定的函数。例如:
onClick = {sum}我们使用:
onClick ={() => sum}Increment: () => dispatch({type: 'INCREMENT'})但不是:
increment: dispatch({type: 'INCREMENT'})问题2:
onClick = {sum()} OR onClick = {sum}问题3:
谢谢你们。非常感谢!
发布于 2020-09-02 08:51:34
对于问题1和问题2,onClick属性需要一个事件处理程序(这是一个函数定义),它将event作为第一个参数,不应该返回任何内容。
单击按钮时,onClick函数将调用该函数,将event作为第一个参数传递。
在在反应中处理事件阅读更多内容
const sum = (a, b) => a + b
// an `event` will be passed as the first argument, the second argument gets undefined
onClick={sum}
// you could opt out the `event` parameter and call `sum` with your arguments
onClick={() => { sum(5, 10) }}
onClick={() => sum(5, 10)} // this will return the value of `sum(5, 10)` to the caller of onClick, which is not useful since the caller don't expect that
=> Using anonymous callback gives you the ability to call the function with specific arguments
// invalid usages, calling the function immediately which will assign the returned value to `onClick`, and the that returned value is not a function definition
onClick={sum(5, 10)}
increment: dispatch({type: 'INCREMENT'})对于问题3,redux-saga充当了一个生成器运行程序,它为您处理了调用next()。您只需要定义生成器函数(结合使用它们的redux-saga效果)
https://stackoverflow.com/questions/63700356
复制相似问题