反应还原和组件与孩子有问题



我认为使用react redux打字有一些地方我不明白。考虑一下:

import React from 'react';
import { Text } from 'react-native';
import { connect } from 'react-redux';
import { AnyAction } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
function SomeComponent_Unconnected(
props: React.PropsWithChildren<{ foo: string; handleSmth: () => void }>
) {
return <>
<Text>{props.foo}</Text>
{props.children}
</>;
}
interface IState {
foo: string;
}
const SomeComponent = connect(
// map state to props
(state: IState) => ({ foo: state.foo }),
// map dispatch to props
(dispatch: ThunkDispatch<IState, any, AnyAction>, getState: () => IState) => ({
handleSmth: () => {
console.log('something happened');
},
}),
)(SomeComponent_Unconnected);
export function AnotherComponent() {
return (
<SomeComponent> {/* TS error here, see below */}
<Text>Test</Text>
</SomeComponent>
);
}

这里是React中Redux的基本用法,但我在AnotherComponent:中遇到了一个奇怪的TS错误

类型"{children:(string|Element([];}"不可分配给类型"((=>IState
类型"{children:(string|Element([];}"不提供与签名"((:IState"匹配的内容。ts(2322(

请注意,如果删除connect函数的mapDispatchToProps部分,则此错误将消失。

你知道这个错误是从哪里来的吗?谢谢

来自redux文档:If your mapDispatchToProps function is declared as taking two parameters, it will be called with dispatch as the first parameter and the props passed to the connected component as the second parameter

它没有您提供的getState参数。您用>过早地关闭了ThunkDispatch的泛型类型参数。

最新更新