javascript - 使用 context api 和 HOC 的不同组件的状态不会更新
问题描述
我正在尝试使用上下文 api 更新应用程序的状态,因为我不需要 redux 的所有功能,也不想处理道具钻孔。因此,我使用 typescript 创建了一个全局上下文和一个包装组件的 HOC 包装器,以便组件类可以访问上下文。
import * as React from 'react';
import GlobalConsumer from './globalConsumer';
import GlobalProvider from './globalProvider';
import IGlobalState from './globalState';
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
type Subtract<T, K> = Omit<T, keyof K>;
export interface IInjectWithState {
globalState: IGlobalState;
}
const withState = <P extends IInjectWithState>(
Component: React.ComponentType<P>
): React.ComponentType<Subtract<P, IInjectWithState>> =>
class WithState extends React.Component<Subtract<P, IInjectWithState>> {
public render(): JSX.Element {
return (
<GlobalProvider>
<GlobalConsumer>
{state => <Component {...this.props} globalState={state} />}
</GlobalConsumer>
</GlobalProvider>
);
}
};
export default withState;
这是HOC。
import * as React from 'react';
import reducer from './reducer';
import IGlobalState from './globalState';
import GlobalContext, { initState } from './globalContext';
class GlobalProvider extends React.Component<{}, IGlobalState> {
constructor(props: any) {
super(props);
this.state = {
...initState,
dispatch: (action: object) =>
this.setState(() => {
return reducer(this.state, action);
})
};
}
public render(): JSX.Element {
return (
<GlobalContext.Provider value={this.state}>
{this.props.children}
</GlobalContext.Provider>
);
}
}
export default GlobalProvider;
这是提供者。
大多数类都包装在 HOC 中,但是每当我调用 dispatch 并更改其中一个组件类中的状态时,全局状态不会在其他组件类中更新。
RootView.tsx:35
{appBarTitle: "Welcome", canContinue: true, currentPage: Array(0), dispatch: ƒ, nextPage: Array(0), …}
ContinueButton.tsx:31
{appBarTitle: "Welcome", canContinue: true, currentPage: Array(0), dispatch: ƒ, nextPage: Array(0), …}
RootView.tsx:39
{appBarTitle: "Welcome", canContinue: true, currentPage: Array(1), dispatch: ƒ, nextPage: Array(1), …}
Start.tsx:21
{appBarTitle: "Welcome", canContinue: true, currentPage: Array(0), dispatch: ƒ, nextPage: Array(0), …}
ContinueButton.tsx:35
{appBarTitle: "Welcome", canContinue: true, currentPage: Array(0), dispatch: ƒ, nextPage: Array(0), …}
组件在根视图中调用 dispatch 后更新,但在另一个类中更新状态后,它不会在其他类中更新。
解决方案
按照您现在设置的方式,使用 HOC 的组件的每个实例都有自己的GlobalProvider
实例,因此也有自己独立的“全局”状态。尝试GlobalProvider
从 HOC 中移除,而是在组件树的最外层添加单个GlobalProvider
组件。
推荐阅读
- php - 更改apache端口后url不起作用?
- elasticsearch - 如何从弹性搜索中的术语聚合查询中获取所有 doc_counts 的总和?
- c# - Xamarin Forms - 检测用户何时开始在滚动视图中向下滚动
- ionic-framework - 离子框架网络插件问题
- python - Django celery 每天在特定时间运行周期性任务
- azure-cosmosdb - 在 Azure 搜索中创建数据源时如何指定特定的 Azure Cosmos Db 区域副本
- ionic3 - 在执行 API 之前等待 Promise 中的值 - 错误:预期 1 个参数,但得到 0
- swift - 从 Vapor 3 中的作业访问服务
- sql - MS Access 对一行中的单列求和
- kotlin - 如何通过上下滑动重新排列我的 recyclerview 项目的顺序?