我正在查看使用react,redux和auth0作爲登錄場景的auth0示例項目here。不過我有點困惑,我們稱之爲this.props.doAuthentication()
對使用react和redux的auth0鎖示例感到困惑
// App.js
import { loginUser, fetchQuote, doAuthentication, fetchSecretQuote } from '../actions'
// add a constructor
constructor(props) {
super(props)
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))
});
});
}
}
...
我是新來的終極版,所以也許這是一個明顯的答案,但
doAuthentication綁定到App.js中的道具在哪裏?假設App.js是頂級的根應用程序組件。
不做認證生成一個需要調度參數的函數嗎?爲什麼我們不使用doAuthentication()的返回函數在構造函數中做任何事情?如果我們不將返回的函數賦值給任何東西,
this.props.doAuthentication
是否會持續存在或有任何影響?它不應該像doAuthentication()(someDispatchFunction)
這樣的調度功能從哪裏來?