JavaScript匿名函数回调和redux saga.next()
我现在已经讨论了几个JavaScript概念,包括几个react表达式,还有几个问题我很好奇,希望能得到你们的帮助 假设我们有一个函数名sum:JavaScript匿名函数回调和redux saga.next(),javascript,reactjs,redux-saga,Javascript,Reactjs,Redux Saga,我现在已经讨论了几个JavaScript概念,包括几个react表达式,还有几个问题我很好奇,希望能得到你们的帮助 假设我们有一个函数名sum: const sum = (a,b) => a + b 问题1: 我看到很多匿名函数被调用来调用另一个函数,我想知道为什么我们这样做而不是直接调用那个特定函数。例如: 而不是使用 我们使用: onClick ={() => sum} 另外,在react课程中,我想知道为什么我们称mapDispatchToProps为: 但不是:
const sum = (a,b) => a + b
问题1:
我看到很多匿名函数被调用来调用另一个函数,我想知道为什么我们这样做而不是直接调用那个特定函数。例如:
- 而不是使用
onClick ={() => sum}
- 另外,在react课程中,我想知道为什么我们称mapDispatchToProps为:
increment: dispatch({type: 'INCREMENT'})
问题2:
- 在单击事件中何时使用sum()或sum,例如:
问题3:
- 正如我们所知,Redux Saga实现了generator函数,但据我所知,generator函数具有多个屈服点时,需要next()才能继续。 然而,在Redux传奇中,我看不到next()的用法,这是因为传奇已经在它的函数中自动调用了next()
谢谢各位。非常感谢 对于问题1和问题2,
onClick
属性需要一个事件处理程序(这是一个函数定义),它将事件作为第一个参数,并且不应返回任何内容
单击按钮时,调用onClick
函数,将事件作为第一个参数传递
阅读更多
对于问题3,redux saga
充当一个生成器运行程序,它为您处理了调用next()的过程。你只需要定义生成器函数(结合使用它们的redux saga
effects)下次请将你的问题分成多篇文章。
Increment: () => dispatch({type: 'INCREMENT'})
increment: dispatch({type: 'INCREMENT'})
onClick = {sum()} OR onClick = {sum}
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'})