首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子组件中父组件的访问状态?

子组件中父组件的访问状态?
EN

Stack Overflow用户
提问于 2018-02-07 11:01:48
回答 5查看 3.2K关注 0票数 0

我刚开始反应js,我需要让另一个类访问组件的状态,我遇到了这个问题,因为我使用原子设计,因为在一个组件中编写所有东西都变成了一个问题,下面是我的代码:Headcomponent

代码语言:javascript
复制
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

代码语言:javascript
复制
   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 (在这里,我需要传递密码的状态)

代码语言:javascript
复制
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状态

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-02-07 11:43:03

headComponent中访问LabeledInput状态的最简单方法是继续传递它。

如果您想从this.state.password内部的headComponent中访问值this.state.password,那么将这个this.state.password作为一个支柱传递给呈现方法中的表单组件。

代码语言:javascript
复制
   render(){
        return (
          <Form 
            fields={this.props.form} 
            buttonText="Submit" 
            password={this.state.password} />
        );
    }

这样,您就可以作为表单组件中的支柱访问this.state.password。然后重复该过程,并将其传递给表单中的LabeledInput组件。

代码语言:javascript
复制
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组件中的值。

票数 1
EN

Stack Overflow用户

发布于 2018-02-07 11:36:41

最合理的方法是将其作为支持传递(您需要的Headcomponent__'s状态的值):

Headcomponent.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
 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组件中。

票数 2
EN

Stack Overflow用户

发布于 2018-02-07 11:08:46

您可以使用props来实现这一点。

根成分:

代码语言:javascript
复制
<div>
  <child myState="hello"></child>
</div>

儿童部分:

代码语言:javascript
复制
<div>
  <child2 myOtherProperty={this.props.myState}></child2>
</div>

Child1组件:

代码语言:javascript
复制
<div>{this.props.myOtherProperty}</div>

您还可以将函数作为属性传递给其他组件,并允许它们在需要时调用根组件,如下所示:

代码语言:javascript
复制
<div>
  <child onChange={this.myOnChangeMethodDefinedInRootComponent.bind(this)}></child>
</div>

这样,您就可以“告诉”根组件,如果在不使用Redux的情况下子程序内部发生了一些变化。

希望这能帮上忙

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48662186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档