j_d*_*j_d 14 ecmascript-6 reactjs react-router redux react-redux
所以我使用的是带有ApiClient助手的react-redux样板.它看起来像这样:
export default class ApiClient {
constructor(req) {
/* eslint-disable no-return-assign */
methods.forEach((method) =>
this[method] = (path, withCredentials, { params, data } = {}) => new Promise((resolve, reject) => {
const request = superagent[method](formatUrl(path))
if (withCredentials) {
console.log('first of all, its true')
console.log(this)
}
if (params) {
request.query(params)
}
if (__SERVER__ && req.get('cookie')) {
request.set('cookie', req.get('cookie'))
}
if (data) {
request.send(data)
}
request.end((err, { body } = {}) => {
return err ? reject(body || err) : resolve(body)
})
}))
/* eslint-enable no-return-assign */
}
/*
* There's a V8 bug where, when using Babel, exporting classes with only
* constructors sometimes fails. Until it's patched, this is a solution to
* "ApiClient is not defined" from issue #14.
* https://github.com/erikras/react-redux-universal-hot-example/issues/14
*
* Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455
*
* Remove it at your own risk.
*/
empty() {}
}
Run Code Online (Sandbox Code Playgroud)
我想将此连接到我的身份验证,以便我可以将标头添加到受保护的端点,如下所示:
@connect(state => ({ jwt: state.auth.jwt }))
export default class ApiClient {
...
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我得到错误:Cannot read property 'store' of undefined.这里发生了什么?为什么我不能将常规类连接到redux商店?
更新:这是我的登录功能,它使用ApiClient助手:
export function loginAndGetFullInto(email, password) {
return dispatch => {
return dispatch(login(email, password))
.then(() => {
return dispatch(loadUserWithAuth())
})
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一些方法来将商店或jwt传递到loadUserWithAuth函数中......
Jim*_*lla 22
该connect函数不适用于React组件以外的任何其他组件.你可以做的是你的存储实例传递给你的类并调用store.dispatch,store.getState和store.subscribe直接.
请记住,如果您订阅,您还应具有取消订阅的功能,否则您的商店将永远保留对您的类实例的引用,从而造成内存泄漏.
| 归档时间: |
|
| 查看次数: |
8299 次 |
| 最近记录: |