我刚开始反应js,我需要让另一个类访问组件的状态,我遇到了这个问题,因为我使用原子设计,因为在一个组件中编写所有东西都变成了一个问题,下面是我的代码: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 (在这里,我需要传递密码的状态)
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;如何在headComponent中访问LabeledInput状态
发布于 2018-02-07 11:43:03
在headComponent中访问LabeledInput状态的最简单方法是继续传递它。
如果您想从this.state.password内部的headComponent中访问值this.state.password,那么将这个this.state.password作为一个支柱传递给呈现方法中的表单组件。
render(){
return (
<Form
fields={this.props.form}
buttonText="Submit"
password={this.state.password} />
);
}这样,您就可以作为表单组件中的支柱访问this.state.password。然后重复该过程,并将其传递给表单中的LabeledInput组件。
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>
);然后,通过调用LabeledInput组件,您可以访问this.props.password组件中的值。
发布于 2018-02-07 11:36:41
最合理的方法是将其作为支持传递(您需要的Headcomponent__'s状态的值):
Headcomponent.js
class Headcomponent extends React.Component {
constructor (props) {
super(props);
this.state = {
email: '',
password: '',
formErrors: {email: '', password: ''},
emailValid: false,
passwordValid: false,
formValid: false,
items: [],
}
}
render() {
return (
<Form
fields={this.props.form}
formValid={this.state.formValid} // from Headcomponent's state
buttonText="Submit"
/>
);
}
}Form.js
const Form = props => (
<form className="Form">
{
props.fields.map((field, i) => (
<LabeledInput
label={field.label}
formValid={props.formValid} // from Headcomponent's state
placeholder={field.placeholder}
key={i}
/>
))
}
<Button text={props.buttonText} />
</form>
);LabeledInput.js
const LabeledInput = props => (
<div className={`form-group `} >
{ props.formValid && 'This form is valid' } // from Headcomponent's state
<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>
);因此,如果Headcomponent的状态被更新,它将被传播到LabeledInput组件中。
发布于 2018-02-07 11:08:46
您可以使用props来实现这一点。
根成分:
<div>
<child myState="hello"></child>
</div>儿童部分:
<div>
<child2 myOtherProperty={this.props.myState}></child2>
</div>Child1组件:
<div>{this.props.myOtherProperty}</div>您还可以将函数作为属性传递给其他组件,并允许它们在需要时调用根组件,如下所示:
<div>
<child onChange={this.myOnChangeMethodDefinedInRootComponent.bind(this)}></child>
</div>这样,您就可以“告诉”根组件,如果在不使用Redux的情况下子程序内部发生了一些变化。
希望这能帮上忙
https://stackoverflow.com/questions/48662186
复制相似问题