【发布时间】:2021-10-21 10:50:23
【问题描述】:
让我们规定一下,我灵魂深处的高深劝告促使我构建了一个 React 组件,该组件包装了对 Redux connect 的调用,如下所示:
// ReduxWrapper.tsx
import React from 'react'
import {connect, useStore} from 'react-redux'
interface SomeOtherComponentProps {
foo?: string,
bar?: string
}
function SomeOtherComponent({foo, bar}: SomeOtherComponentProps) {
return (
<div>
This is a component with foo={foo} and bar={bar}!
</div>
)
}
export default function ReduxWrapper(
child_props: SomeOtherComponentProps
): React.ReactElement {
const store = useStore()
const mapStateToProps = (state: {}) => ({
// @ts-ignore
foo: state.foo
});
return connect(
mapStateToProps, {}
)(SomeOtherComponent)(child_props) as React.ReactElement<any, string | React.JSXElementConstructor<any>>
}
// App.tsx
const store = makeStoreCorrectly()
...
return (
<Provider store={store}>
<ReduxWrapper bar="baz" />
</Provider>
)
出于这个问题的目的,我们不要问为什么我想这样做,或者这是否是个好主意。可以做到吗?我不知道如何在此处正常调用connect。 (在目前的形式中,我得到了一个TypeError: Object(...)(...)(...) is not a function。)
【问题讨论】:
-
这显然是一个xy problem,你知道的:
let's not ask why I want to do this你可能想看看自定义钩子。
标签: reactjs typescript redux react-functional-component