Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用Typescript创建Redux中间件_Reactjs_Typescript_Redux_React Redux - Fatal编程技术网

Reactjs 使用Typescript创建Redux中间件

Reactjs 使用Typescript创建Redux中间件,reactjs,typescript,redux,react-redux,Reactjs,Typescript,Redux,React Redux,我想在我的redux中间件中获取类型检查的状态。使用Typescript 2.6.2,我可以轻松创建一个redux中间件,如下所示: import { Middleware, MiddlewareAPI, Dispatch } from 'redux'; import { IState } from '~/shared/rootReducer'; const logger: Middleware = <S>({ getState }: MiddlewareAPI<S

我想在我的redux中间件中获取类型检查的状态。使用Typescript 2.6.2,我可以轻松创建一个redux中间件,如下所示:

import { Middleware, MiddlewareAPI, Dispatch } from 'redux';
import { IState } from '~/shared/rootReducer';

const logger: Middleware = 
    <S>({ getState }: MiddlewareAPI<S>) => (next: Dispatch<S>) => (action: any) => {
    console.log(action);
    next(action);
};

export default logger;

但我真的不想这样。

据我所知,在版本
redux@^3.7.2
中,不可能在中间件中键入您的状态。以下是一个解决方法,您需要在
applymiddleware
中转换中间件

中间件

据我所知,在版本
redux@^3.7.2
中,不可能在中间件中键入您的状态。以下是一个解决方法,您需要在
applymiddleware
中转换中间件

中间件

不起作用,因为我在
applyMiddleware(…middeware)
中遇到以下错误:
类型“S”不能分配给类型“IState”。
类型定义中的
applyMiddleware
函数没有泛型类型。这是我在
index.d.ts
中看到的:
导出函数applyMiddleware(…中间件:中间件[]):genericstorenhancer,这将解释
TS2558的错误:应为0个类型参数,但得到1。
我得到了正确答案您是对的,我也检查了它。等一下。我还丢失了一个大括号
}现在,您可以将applyMiddleware(…中间件作为中间件[])转换为
并继续使用它。
redux
的4.0版有新的TS定义。我会花更多的时间在它上面,然后回来找一个更好的解决方案。
MiddlewareAPI
签名已更改,现在应该是
MiddlewareAPI
不起作用,因为我在
applyMiddleware(…middeware)中遇到以下错误
类型“S”不可分配给类型“IState”。
其类型定义中的
applyMiddleware
函数没有泛型类型。这是我在
index.d.ts
中看到的:
导出函数applyMiddleware(…中间件:中间件[]):genericstorenhancer,这将解释
TS2558的错误:应为0个类型参数,但得到1。
我得到了正确答案您是对的,我也检查了它。等一下。我还丢失了一个大括号
}现在,您可以将applyMiddleware(…中间件作为中间件[])转换为
并继续使用它。
redux
的4.0版有新的TS定义。我会花更多的时间在这上面,然后回来找一个更好的解决方案。
MiddlewareAPI
签名已经更改,现在应该是
MiddlewareAPI
const state = <IState><any>getState();
import { MiddlewareAPI, Dispatch, Action } from 'redux';
import { IState } from '~/shared/rootReducer';

const logger = (api: MiddlewareAPI<IState>) => {
  const { songs } = api.getState();
  return (next: Dispatch<IState>) => (action: Action) => {
    return next(action);
  };
};

export default logger;
applyMiddleware(...middlewares as Middleware[]);