我需要获取defaultProps
类的要访问的组件的状态,这是我的代码:
class Headcomponent extends React.Component {
constructor(props) {
super(props);
this.state = {
email: '',
password: '',
formErrors: {
email: '',
password: ''
},
emailValid: false,
passwordValid: false,
formValid: false,
items: [],
}
}
this.setState({
formErrors: fieldValidationErrors,
emailValid: emailValid,
passwordValid: passwordValid
}, this.validateForm);
}
validateForm() {
this.setState({
formValid: this.state.emailValid &&
this.state.passwordValid
});
}
render() {
return ( <
Form fields = {
this.props.form
}
buttonText = "Submit" / >
);
}
}
Headcomponent.propTypes = {
form: PropTypes.array,
};
Headcomponent.defaultProps = {
form: [{
label: 'label1',
placeholder: 'Input 1',
value: {
this.state.password
} //this throws an error
},
{
label: 'label2',
placeholder: 'Placeholder for Input 2',
},
],
};
export default Headcomponent;
{this.state.password}
抛出错误,因为它在类之外。我如何获得状态password
并将其传递到内部Headcomponent.defaultProps
?