问题
I have one container component and one form component:
Form component:
import * as React from 'react';
import { reduxForm, Field, InjectedFormProps } from 'redux-form';
export type LoginState = {
email: string;
password: string;
};
interface LoginProps extends InjectedFormProps {
isLoading: boolean;
onSubmit(userCredential: LoginState): void;
}
class Login extends React.Component<LoginProps, LoginState> {
constructor(props: LoginProps) {
super(props);
this.state = {
email: '',
password: '',
otp: '',
};
}
onEmailChange = (e: React.FormEvent<HTMLInputElement>) => {
this.setState({ email: e.currentTarget.value.trim() });
};
onPasswordChange = (e: React.FormEvent<HTMLInputElement>) => {
this.setState({ password: e.currentTarget.value.trim() });
};
onSubmit = () => {
this.props.onSubmit(this.state);
};
render() {
return (
<div>
<h3 className="ac-section__title">Login</h3>
<div className="ac-section__body">
<form onSubmit={this.props.handleSubmit(this.onSubmit)}>
<Field
name="email"
placeholder="Email"
component={renderInput}
type="email"
value={this.state.email}
onChange={this.onEmailChange}
validate={[required, email]}
/>
<Field
name="password"
placeholder="Password"
component={renderInput}
type="password"
value={this.state.password}
onChange={this.onPasswordChange}
validate={required}
/>
<br />
<div className="loginBtnContainer">
<button className="btn primaryButton" type="submit">
Login
</button>
</div>
</form>
<div className="ac-password__wrapper">
<Link to="/forgot-password" className="ac-password-link">
Forgot your password?
</Link>
</div>
</div>
</div>
);
}
}
export default reduxForm({ form: 'loginForm' })(Login);
Container component:
return (
<div>
<Login onSubmit={this.onSubmit} isLoading={true} />
/* here throwing error: Property 'isLoading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<FormInstance<{}, Partial<ConfigProps<{}, {}>>>> & ...'. */
</div>
);
here isLoading
is not passing as a props to form component. please help me out with a good solution.
Thanks :)
回答1:
Redux Form is a very efficient approach for form handling and validation in React + Redux environments. Here the issue you are having looks to be about missing props, and a few interface definitions. I have provided a sandbox at the end.
A solution could be to specify the form interface by yourself, something like this:
export interface FormInterface {
handleSubmit: PropTypes.func;
handleChange?: PropTypes.func;
reset?: PropTypes.func;
pristine?: PropTypes.bool;
submitting?: PropTypes.bool;
error?: PropTypes.bool;
invalid?: PropTypes.bool;
submitSucceeded?: PropTypes.bool;
submitFailed?: PropTypes.bool;
anyTouched?: PropTypes.bool;
}
and use that to derive LoginProps
from. Then typescript won't complain about missing props, which is apparently the way redux form works.
See this sandbox for details. I have gone over a few minor stuff for you, as well.
Hope this helps.
来源:https://stackoverflow.com/questions/48651100/property-isloading-does-not-exist-on-type-intrinsicattributes-intrinsicclas