reactjs - 将高阶组件与连接到 Redux 存储的现有类组件一起使用
问题描述
所以我对 React 很陌生,我正在做一个使用 Redux 的项目。任务是获取 Okta 上下文以访问身份验证令牌。
问题是我有这个来自 Okta 的例子
import fetch from 'isomorphic-fetch';
import React, { Component } from 'react';
import { withOktaAuth } from '@okta/okta-react';
export default withOktaAuth(class MessageList extends Component {
constructor(props) {
super(props)
this.state = {
messages: null
}
}
async componentDidMount() {
try {
const response = await fetch('http://localhost:{serverPort}/api/messages', {
headers: {
Authorization: 'Bearer ' + this.props.authState.accessToken
}
});
const data = await response.json();
this.setState({ messages: data.messages });
} catch (err) {
// handle error as needed
}
}
render() {
if (!this.state.messages) return <div>Loading...</div>;
const items = this.state.messages.map(message =>
<li key={message}>{message}</li>
);
return <ul>{items}</ul>;
}
});
...他们在其中使用和导出 withOktaAuth 高阶组件。我无法使用现有代码执行此操作,因为它是作为类组件编写的:
class FetchData extends React.PureComponent<InspectionBrowserProps> {
// This method is called when the component is first added to the document
public componentDidMount() {
this.ensureDataFetched();
}
// This method is called when the route parameters change
public componentDidUpdate() {
}
public render() {
--- render stuff--
}
}
}
然后通过连接导出到 redux 存储
export default connect(
(state: ApplicationState) => state.inspectionBrowser, // Selects which state properties are merged into the component's props
InspectionBrowserStore.actionCreators // Selects which action creators are merged into the component's props
)(FetchData as any);
那么如何使用 withOktaAuth 组件获取访问令牌,但仍然将现有组件作为类连接到 Redux 存储?感觉就像苹果和橘子对我来说有点问题。
解决方案
推荐阅读
- python - 使用python从列表中获取最长的字符串
- salesforce - Salesforce 营销云 - 自定义旅程活动端点未执行
- python - 强制 virtualenvironment 创建一个空的环境
- css - S3 上的自托管 FontAwesome
- java - 将 double 数组转换为 longss 数组,以保持相对性
- lua - How to detect right-click on DCollapsibleCategory? [GLua]
- php - 为什么这个模态表单被重定向到我的 php 文件?
- shell - 如何在 AppShell Xamarin 表单中制作透明导航栏
- java - 当属性具有 @XmlJavaTypeAdapter 注释时,使用 Swagger/Springfox 更正数据类型
- java - OnClickListener 类不打开相机