子组件中父组件的访问状态? [英] Access state of parent component in a child component?
问题描述
我是新来响应js的人,我需要获取要由另一个类访问的组件的状态,我遇到了这个问题,因为我正在使用原子设计,因为在一个组件中编写所有内容都变成了问题,这是我的代码: Headcomponent :
I'm new to react js and I need to get the state of the component to be accessed by another class, I encountered this problem because I'm using atomic design because writing everything in one component is turning to be a problem, here is my code: Headcomponent:
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',
},
{
label: 'label2',
placeholder: 'Placeholder for Input 2',
},
],
};
export default Headcomponent;
form.js
const Form = props => (
<form className="Form">
{
props.fields.map((field, i) => (<LabeledInput label={field.label} placeholder={field.placeholder} key={i}/>))
}
<Button text={props.buttonText} />
</form>
);
Form.propTypes = {
fields: PropTypes.arrayOf(PropTypes.object).isRequired,
buttonText: PropTypes.string.isRequired,
};
export default Form;
LabeledInput.js (我需要传递密码状态的地方)
LabeledInput.js (where I need to pass the state of my password)
const LabeledInput = props => (
<div className={`form-group `} >
<Label text={props.label} />
<Input value={props.value} placeholder={props.placeholder} type="text" onChange={props.onChange} />
<div class="valid-feedback">{props.errorMessage}</div>
<small class="form-text text-muted">{props.exampleText}</small>
</div>
);
LabeledInput.propTypes = {
label: PropTypes.string.isRequired,
placeholder: PropTypes.string,
onChange: PropTypes.func.required,
value: PropTypes.string.isRequired,
exampleText: PropTypes.string,
errorMessage: PropTypes.string,
};
export default LabeledInput;
如何在LabeledInput
中访问headComponent的状态?
How can I access state of headComponent in LabeledInput
?
推荐答案
访问LabeledInput
中的headComponent
状态以保持向下传递的最简单方法.
The simplest way to access the state of headComponent
in LabeledInput
in to keep passing it down.
如果要从LabeledInput
内部的headComponent
访问值this.state.password
,则可以将此this.state.password
作为属性传递给render方法中的表单组件
If you want to access the value this.state.password
from headComponent
inside LabeledInput
then you pass this this.state.password
as a prop to the form component in the render method
render(){
return (
<Form
fields={this.props.form}
buttonText="Submit"
password={this.state.password} />
);
}
然后,您可以在表单组件内作为道具访问this.state.password
.然后重复该过程并将其传递给表单内的LabeledInput
组件
This then gives you access to this.state.password
as a prop inside the Form component. You then repeat the process and pass it to the LabeledInput
component inside form
const Form = props => (
<form className="Form">
{
props.fields.map((field, i) => (
<LabeledInput
label={field.label}
placeholder={field.placeholder}
key={i}
password={this.props.password}
/>))
}
<Button text={props.buttonText} />
</form>
);
然后,您可以通过调用this.props.password
来访问LabeledInput
组件内的值.
This then gives you access to the value inside the LabeledInput
component by calling this.props.password
.
这篇关于子组件中父组件的访问状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!