あなたは、アプリケーション全体の状態を表すインターフェイスを作成する必要があります。
interface ApplicationState {
someProp1: {
someProp1a: string;
someProp1b: number;
};
someProp2: {
someProp1a: string;
someProp1b: number;
};
}
次に、各スマートコンポーネントの状態を表し、一つのインタフェースを作成します(mapStateToProps
経由で店舗に接続されているコンポーネント):
interface SomeComponentState {
someProp1: {
someProp1a: string;
someProp1b: number;
};
}
MyComponentState
インターフェイスは、AppState
のサブセットである必要があります。
type SomeComponentProps = Pick<ApplicationState, "someProp1">;
ます。また、スマートコンポーネントのアクションの種類を宣言する必要があります:これは、あなたが実際に行うことができることを意味し
const actionsCreators = {
doSomething: (txt: string) => ({ type: "DO_SOMETHING", pleyload: txt })
};
type SomeComponentActions = { actions: typeof actionsCreators };
スマートコンポーネントのプロパティは、のタイプの労働組合でありますそのプロパティとそのアクション:SomeComponentProps & SomeComponentActions
。
class MyComponent extends React.Component<SomeComponentProps & SomeComponentActions, void> {
public render() {
return <div onClick={() => this.props.actions.doSomething(this.props.someProp1.someProp1a)} >Click me!</div>;
}
}
あなたは、コンポーネントの状態にアプリケーションの状態からマッピングする:
function mapStateToProps(state: ApplicationState): SomeComponentProps {
return {
someProp1: state.someProp1
};
}
function mapDispatchToProps(dispatch: Redux.Dispatch<typeof actionsCreators>) {
return { actions : bindActionCreators(actionsCreators, dispatch) };
}
const MySmarthComponent = connect(mapStateToProps, mapDispatchToProps)(MyComponent);
はこのくらいを破壊いただき、誠にありがとうございます。しかし、 'mapStateToProps'の戻り値の型を' someComponentProps'に設定しようとすると、私はまだエラーが発生します。それは私が 'any'に設定したときのみ機能します。ただし、私はまだ何もアクションを設定していないことに注意してください – Lexcorp16