获取 props.dispatch() 的类型定义,以便将 TypeScript 与 React-Redux 结合使用
Get type definition for props.dispatch() for using TypeScript with React-Redux
我有这个 functional/stateless 组件:
import React from 'react';
import {useFormik} from 'formik';
import {connect} from "react-redux";
function mapStateToProps(){
return {
foo: "bar"
}
}
interface OwnProps {
propFromParent: number
}
type StateProps = ReturnType<typeof mapStateToProps>
type Props = StateProps & OwnProps
const SignupForm = (props: Props) => {
const formik = useFormik({
initialValues: {
email: '',
name: '',
password: ''
},
onSubmit(values) {
props.dispatch() // props.dispatch is not defined!
}
});
return (
<form onSubmit={formik.handleSubmit}>
<label htmlFor="name">Full Name</label>
<input
id="name"
name="name"
type="name"
onChange={formik.handleChange}
value={formik.values.name}
/>
<button type="submit">Submit</button>
</form>
);
};
export default connect<StateProps, null, Props>(mapStateToProps)(SignupForm);
所以我得到了这个编译错误:
那么如何包含类型定义以便定义 props.dispatch?
只是寻求有关正确 TS 定义的帮助。
您需要添加一个新函数并将其作为第二个参数传递给 connect()
...
function mapDispatchToProps(dispatch): IDispatchProps {
return {
dispatch
};
}
connect(mapStateToProps, mapDispatchToProps)
请参阅 React-Redux docs on "Static Typing",它展示了如何为 connect
将传递到您的组件的内容定义正确的类型。
具体来说,我们建议使用 ConnectedProps<T>
助手,如下所示:
import { connect, ConnectedProps } from 'react-redux'
interface RootState {
isOn: boolean
}
const mapState = (state: RootState) => ({
isOn: state.isOn
})
const mapDispatch = {
toggleOn: () => ({ type: 'TOGGLE_IS_ON' })
}
const connector = connect(
mapState,
mapDispatch
)
// The inferred type will look like:
// {isOn: boolean, toggleOn: () => void}
type PropsFromRedux = ConnectedProps<typeof connector>
interface Props extends 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)
我有这个 functional/stateless 组件:
import React from 'react';
import {useFormik} from 'formik';
import {connect} from "react-redux";
function mapStateToProps(){
return {
foo: "bar"
}
}
interface OwnProps {
propFromParent: number
}
type StateProps = ReturnType<typeof mapStateToProps>
type Props = StateProps & OwnProps
const SignupForm = (props: Props) => {
const formik = useFormik({
initialValues: {
email: '',
name: '',
password: ''
},
onSubmit(values) {
props.dispatch() // props.dispatch is not defined!
}
});
return (
<form onSubmit={formik.handleSubmit}>
<label htmlFor="name">Full Name</label>
<input
id="name"
name="name"
type="name"
onChange={formik.handleChange}
value={formik.values.name}
/>
<button type="submit">Submit</button>
</form>
);
};
export default connect<StateProps, null, Props>(mapStateToProps)(SignupForm);
所以我得到了这个编译错误:
那么如何包含类型定义以便定义 props.dispatch? 只是寻求有关正确 TS 定义的帮助。
您需要添加一个新函数并将其作为第二个参数传递给 connect()
...
function mapDispatchToProps(dispatch): IDispatchProps {
return {
dispatch
};
}
connect(mapStateToProps, mapDispatchToProps)
请参阅 React-Redux docs on "Static Typing",它展示了如何为 connect
将传递到您的组件的内容定义正确的类型。
具体来说,我们建议使用 ConnectedProps<T>
助手,如下所示:
import { connect, ConnectedProps } from 'react-redux'
interface RootState {
isOn: boolean
}
const mapState = (state: RootState) => ({
isOn: state.isOn
})
const mapDispatch = {
toggleOn: () => ({ type: 'TOGGLE_IS_ON' })
}
const connector = connect(
mapState,
mapDispatch
)
// The inferred type will look like:
// {isOn: boolean, toggleOn: () => void}
type PropsFromRedux = ConnectedProps<typeof connector>
interface Props extends 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)