Reactjs 对使用react和redux的auth0锁示例感到困惑
我正在浏览auth0示例项目,了解如何在登录场景中使用react、redux和auth0。然而,我对这个特殊的例子有点困惑,在这个例子中我们调用Reactjs 对使用react和redux的auth0锁示例感到困惑,reactjs,redux,auth0,Reactjs,Redux,Auth0,我正在浏览auth0示例项目,了解如何在登录场景中使用react、redux和auth0。然而,我对这个特殊的例子有点困惑,在这个例子中我们调用this.props.doAuthentication() 下面是动作定义 // actions.js ... const lock = new Auth0Lock('YOUR_CLIENT_ID', 'YOUR_CLIENT_DOMAIN'); export function login() { // display lock widget
this.props.doAuthentication()
下面是动作定义
// actions.js
...
const lock = new Auth0Lock('YOUR_CLIENT_ID', 'YOUR_CLIENT_DOMAIN');
export function login() {
// display lock widget
return dispatch => {
lock.show();
}
}
// Listen to authenticated event and get the profile of the user
export function doAuthentication() {
return dispatch => {
lock.on("authenticated", function(authResult) {
lock.getProfile(authResult.idToken, function(error, profile) {
if (error) {
// handle error
return dispatch(lockError(error))
}
localStorage.setItem('profile', JSON.stringify(profile))
localStorage.setItem('id_token', authResult.idToken)
return dispatch(lockSuccess(profile))
});
});
}
}
...
我是redux的新手,所以也许这是一个显而易见的答案,但是
this.props.doAuthentication
是否会保留任何对象或产生任何效果?它不应该类似于doAuthentication()(someDispatchFunction)
这个分派函数来自哪里doAuthentication
通过调用的中间件与App
组件的props
绑定
上面的两行代码可以为您的应用程序完成这项工作。请阅读为什么需要使用redux thunk
2.doAuthentication不生成需要分派参数的函数吗?为什么我们不使用doAuthentication()返回的函数在构造函数中执行任何操作?如果我们没有将返回的函数分配给任何对象,那么this.props.doAuthentication是否会保留任何对象或产生任何影响?它不应该类似于doAuthentication()(someDispatchFunction)吗?这个分派函数来自哪里
Ans:
2.1是的,doAuthentication
函数返回的函数期望dispatch
方法作为第一个参数
2.2,2.3此处,doAuthentication
操作创建者的工作是创建一个Redux操作
,该操作只侦听名为authenticated
的事件。当我们调用doAuthentication action creator时,它返回一个Redux action
函数,该函数接受dispatch
方法作为第一个参数,接受getState
方法作为第二个参数。参数将由redux thunnk
中间件传递
redux-thunk
中间件将调用从doAuthentication
调用返回的redux操作,因为它是connet
-ed。否则,我们必须按如下所示调度doAuthentication
返回的操作
this.props.dispatch(doAuthentication())
2.4,我们可以按照你提到的<代码> doAudioCuto()(某些Debug函数)< /C> >,但是考虑这个引用
如果启用了Redux Thunk中间件,则在尝试 分派一个函数而不是一个动作对象,中间件将 使用dispatch方法本身作为第一个参数调用该函数 而且,你可以通过这个redux thunk
找到详细信息。doAuthentication绑定到App.js中的道具在哪里?假设App.js是顶级根应用程序组件
Ans:
动作doAuthentication
通过调用的中间件与App
组件的props
绑定
上面的两行代码可以为您的应用程序完成这项工作。请阅读为什么需要使用redux thunk
2.doAuthentication不生成需要分派参数的函数吗?为什么我们不使用doAuthentication()返回的函数在构造函数中执行任何操作?如果我们没有将返回的函数分配给任何对象,那么this.props.doAuthentication是否会保留任何对象或产生任何影响?它不应该类似于doAuthentication()(someDispatchFunction)吗?这个分派函数来自哪里
Ans:
2.1是的,doAuthentication
函数返回的函数期望dispatch
方法作为第一个参数
2.2,2.3此处,doAuthentication
操作创建者的工作是创建一个Redux操作
,该操作只侦听名为authenticated
的事件。当我们调用doAuthentication action creator时,它返回一个Redux action
函数,该函数接受dispatch
方法作为第一个参数,接受getState
方法作为第二个参数。参数将由redux thunnk
中间件传递
redux-thunk
中间件将调用从doAuthentication
调用返回的redux操作,因为它是connet
-ed。否则,我们必须按如下所示调度doAuthentication
返回的操作
this.props.dispatch(doAuthentication())
2.4,我们可以按照你提到的<代码> doAudioCuto()(某些Debug函数)< /C> >,但是考虑这个引用
如果启用了Redux Thunk中间件,则在尝试 分派一个函数而不是一个动作对象,中间件将 使用dispatch方法本身作为第一个参数调用该函数 此外,您还可以使用此工具找到有关redux thunk
的详细信息
this.props.dispatch(doAuthentication())