在道具内部无法访问redux派发方法。从redux-form调用handleSubmit时,dispatch方法返回“ dispatch不是函数”。
我尝试将操作添加到要传递给组件的各种接口中。我要传递给connect的参数顺序看起来正确,以及我用来将操作传递给要传递给组件的接口的语法。
PropsFromDispatch包含loginRequest操作,我正在MapDispatchToProps方法中将其映射到props。
export interface IAppProps {
loggingIn: any;
}
export interface IAppState {
user: GetTokenRequest;
submitted: boolean;
validate: {
emailState:string
}
}
interface PropsFromDispatch {
loginRequest: (username: string, password:string) => void;
}
type AllProps = IAppProps & PropsFromDispatch & ConnectedReduxProps &
IAppState
export default class LoginPage extends React.Component<AllProps, IAppState,
PropsFromDispatch> {
constructor(props: AllProps) {
super(props);
this.state = this.getInitState();
this.handleUserNameChange = this.handleUserNameChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
private getInitState(): IAppState {
//Where I'm getting the error
handleSubmit(e: { preventDefault: () => void; }) {
e.preventDefault();
this.setState({ submitted: true });
const { username, password } = this.state;
const {dispatch} = this.props
if (username && password) {
this.props.loginRequest(username, password)
//have also tried
dispatch(loginRequest(username,password))
}
}
render() {
const {loggingIn} = this.props
const {username, password} = this.state;
return (
<Container>
<h3>Sign In</h3>
<Form>
<Col>
<FormGroup>
<Label>Username</Label>
<Input
name="email"
id="exampleEmail"
placeholder="firstname.lastname"
value={ username }
onChange={ (e) => {this.handleUserNameChange(e)} } />
<FormFeedback valid>
Valid Username
</FormFeedback>
<FormFeedback>
Invalid UserName
</FormFeedback>
<FormText>Windows Login</FormText>
</FormGroup>
</Col>
<Col>
<FormGroup>
<Label for="password">Password</Label>
<Input
type="password"
name="password"
id="password"
placeholder="********"
value={ password }
onChange={ (e) => this.handlePasswordChange(e) }
/>
</FormGroup>
</Col>
<Button onClick={(e: any) =>
this.handleSubmit(e)}>Submit</Button>
</Form>
</Container>
);
}
}
function mapStateToProps(state: { authentication: { loggingIn: any; }; }) {
const { loggingIn } = state.authentication;
return {
loggingIn
};
}
const mapDispatchToProps = (dispatch: Dispatch<any>): PropsFromDispatch =>
{
return {
loginRequest: (username: string, password: string) =>
dispatch(loginRequest(username,password))
}
}
const connectedLoginPage = connect(mapStateToProps, mapDispatchToProps)
(LoginPage);
export { connectedLoginPage as LoginPage };