[英]How to disable a button from react component inherited by a child react component?
I have a child react component that extends a parent react component.我有一个扩展父反应组件的子反应组件。
The child component is a form and it inherits a button that saves the form from parent.子组件是一个表单,它从父组件继承了一个保存表单的按钮。
In some situations, if validation on the form fails in child component, the save button from parent component should be disabled.在某些情况下,如果子组件中的表单验证失败,则应禁用父组件的保存按钮。
How do I disable that parent button from the child component?如何从子组件禁用该父按钮?
this is the parent's render function looks like:这是父级的渲染函数,如下所示:
render() {
return (
<Modal open={this.props.open} onClose={this.props.onClose.bind(this.props.parent)}>
<Modal.Content>
<Modal.Description>
{this.getDescription()}
</Modal.Description>
</Modal.Content>
<Modal.Actions>
<Button className="buttontype1" onClick={this.props.onSave.bind(this.props.parent)}>{this.getLocalValue('savebutton', 'Save')}</Button>
<Button className="buttontype2" onClick={this.props.onClose.bind(this.props.parent)}>{this.getLocalValue('cancelbutton', 'Cancel')}</Button>
</Modal.Actions>
</Modal>
);
} }
or if it's possible to overwrite the parent's onSave?或者是否可以覆盖父母的 onSave?
thanks.谢谢。
you may set up a state in the parent component and pass this condition to the child button, like this:您可以在父组件中设置一个状态并将此条件传递给子按钮,如下所示:
parent:家长:
import React, { useState } from 'react';
import Child from '../path-to/Child';
const Parent = () => {
const [condition, setCondition] = useState(false);
return <Child condition={condition} />;
}
**child: ** **孩子: **
import React, { useState } from 'react';
const Child = ({condition, setParent}) => {
const handleValidate = () => {
let condition = true;
setParent(condition);
}
return <button disabled={condition} setParent={setCondition} />;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.