我有一个父组件和几个子组件。我需要根据 ErrorComponent 禁用或启用父级中的按钮。如果出现错误,则禁用该按钮或启用它。我相信我们可以将回调从孩子传递给父母,让父母知道并更新按钮属性。我需要知道如何使用 React 钩子做同样的事情?我尝试了几个例子,但徒劳无功。错误组件上没有事件。如果出现错误 (props.errorMessage),那么我需要将一些数据传递给父级,以便我可以禁用该按钮。任何帮助表示高度赞赏
export const Parent: React.FC<Props> = (props) => {
....
const createContent = (): JSX.Element => {
return (
{<ErrorPanel message={props.errorMessage}/>}
<AnotherComponent/>
);
}
return (
<Button onClick={onSubmit} disabled={}>My Button</Button>
{createContent()}
);
};
export const ErrorPanel: React.FC<Props> = (props) => {
if (props.message) {
return (
<div>{props.message}</div>
);
}
return null;
};