当调用组件时,“ redux”具有“连接”并且调用时需要转移属性,短绒棉将在没有属性的情况下发誓
Component1
std::copy
Component2
interface Component1Props {
id: numbers;
users: any[];
}
const Component1: React.FC<Component1Props> = ({
id,
users
}) => {
...
};
const mapStateToProps = ({ users }: any) => ({
users
});
export default connect(mapStateToProps, null)(Component1);
如何解决?
答案 0 :(得分:0)
您可能要使用他们官方文件中的这种方法
typing-the-connect-higher-order-component
ConnectedProps
使用
ConnectedProps<T>
导出的@types/react-redux^7.1.2
类型从connect
推断道具的类型。
import { connect, ConnectedProps } from 'react-redux'
interface RootState {
}
const mapState = (state: RootState) => ({
})
const mapDispatch = {
}
const connector = connect(mapState, mapDispatch)
type PropsFromRedux = ConnectedProps<typeof connector>
type Props = PropsFromRedux & {
backgroundColor: string
}
const MyComponent = (props: Props) => (
<div style={{ backgroundColor: props.backgroundColor }}>
<button onClick={props.toggleOn}>
Toggle is {props.isOn ? 'ON' : 'OFF'}
</button>
</div>
)
export default connector(MyComponent)