简体   繁体   English

在父组件之间切换布尔状态

[英]Toggle boolean state between from parent component

Looking for a way to toggle a boolean state value in a child component from a method in the Parent component. 寻找一种从父组件中的方法切换子组件中的布尔状态值的方法。

The Parent contains a method to open the contact form into view. 父级包含一种用于打开联系表的方法。

  handleContact() {
    this.setState({
      contactOpen: !this.state.contactOpen,
    });
  }

The ContactForm child component has a state for thankYou (boolean value) which gets set to true upon form submit. ContactForm子组件的状态为thankYou (布尔值),该状态在表单提交时设置为true。

The issue is when the Parent method to open the contact form is called after the form is already submitted, the Thank You message stays on the screen. 问题是在提交表单后调用了用于打开联系人表单的Parent方法时,“谢谢”消息仍停留在屏幕上。

I have tried moving the thankYou state to the Parent and creating a showThankYou method in Parent and passing it to ContactForm child. 我尝试将thankYou状态移动到Parent,并在Parent中创建一个showThankYou方法并将其传递给ContactForm子级。 The function does not seem to run and this way seems like it might be overly-complicated. 该函数似乎没有运行,并且这种方式似乎过于复杂。

Is there a another way to update the boolean state in the child component from the handleContact method in the Parent? 还有另一种方法可以从父级的handleContact方法更新子级组件中的布尔状态吗?

I suggest you to have one childComponent for your form and a childComponent/(just a jsx) for your thank you message and render both in the parent component like this: 我建议您为表单提供一个childComponent,并为您的感谢消息提供一个childComponent /(只是一个jsx),并在父组件中进行呈现,如下所示:

// Parent
constructor(props) {
    super(props);

    this.state = {
        contactOpen: true,
        showThankU: false
    }
}

onSubmit(params) {
   // Do the submission process

   this.setState({showThankU: true, contactOpen: false}, () => {
       // in the callback use setTimout
       // to hide thank you box after desired seconds

       setTimeout(() => this.setState({showThankU: false), 10000);
   });
}

render() {
    return (
        <div>
            {/* if you would like to hide it after submit use this.state.contactOpen && */}
            {this.state.contactOpen && 
              <ContactsForm onSubmit={this.onSubmit.bind(this)}/>
            }

            {/* Whatever your thank you message (childComponet or just jsx) is: */}
            {this.state.showThankU && <ThankYouMessage {...props} />
        </div>
    );
}

if you would like to handle all in contactForm component you should pass a callback from parent to the child (contactForm) and change the state of showing the message or not and also pass that as another props to contactForm: 如果您想处理contactForm组件中的所有内容,则应将回调从父级传递给子级(contactForm),并更改是否显示消息的状态,并将其作为另一个支持传递给contactForm:

<ContactsForm 
    showMessage={this.state.showMessage}
    onSubmit={() => {
         this.setState({showMessage: true});
    }}
    {/* Other props */}
/>

And also keep in mind that when you want to pass a callback to child , you should be careful with this of JavaScript: 还要记住,当您要将回调传递给child时,应注意以下 JavaScript:

for that you could bind your methods like these: 为此,您可以绑定以下方法:

// in constructor
this.myMethod = this.myMethod.bind(this)

// Or use ES6 and callback
<ChildComponent 
    myProp={() => this.myMethod()}
/>

// Or bind when passing to child
<ChildComponent
    myProp={this.myMethod.bind(this)}
/>
    handleContact() {
    const contact = !this.state.contactOpen;
    this.setState({
      contactOpen: contact
    });
  }

Fix by Nicholas ( https://stackoverflow.com/users/2836350/nicholas ) (I think I already tried this approach and didn't work for me, that's why i use the one from above): 由Nicholas( https://stackoverflow.com/users/2836350/nicholas )修复(我想我已经尝试过这种方法,但对我不起作用,这就是为什么我使用上面的方法):

handleContact=()=> this.setState(prevState=>({contactOpen: !prevState.contactOpen}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM