【发布时间】:2020-01-30 16:46:12
【问题描述】:
有没有办法使用mapStateToProps和mapDispatchToProps的类型自动扩展连接组件的接口?例如以下代码:
interface ComponentProps {
state?: State;
action?: (id: string) => void;
}
const mapStateToProps = (state: any) => ({
state: state,
});
const mapDispatchToProps = (dispatch: any) => ({
action: (id: string) => dispatch(Action),
});
const Component = (props: ComponentProps) => <div>...</div>;
export const ConnectedComponent = connect(
mapStateToProps,
mapDispatchToProps,
)(Component);
要求我将state 和action 作为可选道具添加到我的ComponentProps 以便在我的组件中使用它们,因为道具将由connect HOC 分配。
当使用 materialUI 及其withStyles HOC 之类的东西时,我们可以使用WithStyles<typeof styles> 自动将classes 属性(确切的键取决于styles)添加到我们的界面中,例如
ComponentProps extends WithStyles<typeof styles> {
actualProps: any;
}
const ConnectedComponent = withStyles(styles)(Component);
是否可以为connect 做同样的事情?
【问题讨论】:
标签: javascript reactjs typescript redux redux-thunk