我正在为我当前的 web 应用程序使用 redux 和 typescript。
定义通过 接收 redux-actions 的组件的 props 的最佳实践是什么@connect?例子:
// mychild.tsx
export namespace MyChildComponent {
  export interface IProps {
    propertyFromParent: string;
    propertyFromRedux: string;  // !!!! -> This is the problem
    actionsPropertyFromRedux: typeof MyReduxActions;  // !!!! -> And this     
  }
}
@connect(mapStateToProps, mapDispatchToProps)
export class MyChildComponent extends React.Component <MyChildComponent.IProps, any> {
... react stuff
}
function mapStateToProps(state: RootState) {
  return {
    propertyFromRedux: state.propertyFromRedux
  };
}
function mapDispatchToProps(dispatch) {
  return {
    actionsPropertyFromRedux: bindActionCreators(MyReduxActions as any, dispatch)
  };
}
// myparent.tsx
export class MyParentComponent extends React.Component <MyParentComponent.IProps, any> {
... react stuff
    render(){
        // typescript complains, because I am not passing `propertyFromRedux`! 
        return <div><MyChildComponent propertyFromParent="yay" /></div>;
    }
}
正如我所见,我有 2 个解决方案。
只需通过我的整个应用程序传递操作和状态即可。但这意味着即使只有一些小的子组件需要更改,我的整个应用程序也会重新渲染。或者它是在我的顶级组件中监听所有存储更改的 redux 方式吗?然后我将不得不为
shouldComponentUpdate不是平面对象的props编写很多逻辑。设置帕拉姆中
IProps的MyChildComponent这样的可选:
——
// mychild.tsx
export namespace MyChildComponent {
  export interface IProps {
    propertyFromParent: string;
    propertyFromRedux?: typeof MyAction;  // This is the problem
  }
}
还有其他方法吗?以上两种方式在我看来都太凌乱了。