简体   繁体   English

如何禁用子反应组件继承的反应组件中的按钮?

[英]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.

相关问题 如何在反应子组件中调用继承的函数 - How to call inherited function in react child component 如何在子组件上禁用PanResponder-React Native - How To Disable PanResponder on Child Component - React Native 如何禁用导入到另一个组件的 React 中的按钮? - how to disable a button in React imported into another component? React Native ScrollView-如何从另一个子组件按钮滚动到一个子组件? - React Native ScrollView - How to scroll to a child component from another child component button? 如何从父反应组件访问子反应组件中的数据 - How to access data in a child react component from a parent react component 单击子组件按钮时卸载组件 // 反应 - Unmount component on click in child component button // React 如何从父组件中访问子组件的子进程? - How to access child component's child from parent component in react? 如何将 onClick() 事件从 React 中父组件中的按钮传递给子组件? - How to pass the onClick() event to a child component from a button in the parent component in React? 如何从继承的类覆盖 React.Component 状态类型? - How to overwrite React.Component state type from inherited class? 如何从React组件读取子元素 - How to read child elements from a React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM