简体   繁体   English

如何使用反应钩子将数据从子组件传递到父组件

[英]How to pass data from child to parent component using react hooks

I have a Parent component and couple of child components.我有一个父组件和几个子组件。 I need to disable or enable the button in the parent based on the ErrorComponent.我需要根据 ErrorComponent 禁用或启用父级中的按钮。 If there is an error then I disable the button or else I enable it.如果有错误,那么我禁用该按钮,否则我启用它。 I believe we can pass callbacks from the child to parent and let the parent know and update the button property.我相信我们可以将回调从子级传递给父级,让父级知道并更新按钮属性。 I need to know how to do the same using react hooks?我需要知道如何使用反应钩子来做同样的事情? I tried few examples but in vain.我尝试了几个例子但徒劳无功。 There is no event on error component.错误组件上没有事件。 If there is an error (props.errorMessage) then I need to pass some data to parent so that I can disable the button.如果出现错误(props.errorMessage),那么我需要将一些数据传递给父级,以便我可以禁用该按钮。 Any help is highly appreciated非常感谢任何帮助

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;
};

I'd use useEffect hook in this case, to set the disabled state depending on the message props.在这种情况下,我将使用useEffect挂钩,根据message道具设置disabled的 state。 You can see the whole working app here: codesandbox你可以在这里看到整个工作应用程序: codesandbox

ErrorPanel component will look like this: ErrorPanel组件将如下所示:

import React, { useEffect } from "react";

interface IPropTypes {
  setDisabled(disabled:boolean): void;
  message?: string;
}

const ErrorPanel = ({ setDisabled, message }: IPropTypes) => {

  useEffect(() => {
    if (message) {
      setDisabled(true);
    } else {
      setDisabled(false);
    }
  }, [message, setDisabled]);

  if (message) {
    return <div>Error: {message}</div>;
  }
  return null;
};

export default ErrorPanel;

So depending on the message prop, whenever it 'exists', I set the disabled prop to true by manipulating the setDisabled function passed by the prop.因此,根据message道具,只要它“存在”,我就会通过操纵道具传递的setDisabled function 将disabled道具设置为 true。

And to make this work, Parent component looks like this:为了使它工作, Parent组件看起来像这样:

import React, { MouseEvent, useState } from "react";

import ErrorPanel from "./ErrorPanel";

interface IPropTypes {
  errorMessage?: string;
}

const Parent = ({ errorMessage }: IPropTypes) => {
  const [disabled, setDisabled] = useState(false);

  const createContent = () => {
    return <ErrorPanel setDisabled={setDisabled} message={errorMessage} />;
  };

  const handleSubmit = (e: MouseEvent) => {
    e.preventDefault();
    alert("Submit");
  };

  return (
    <>
      <button onClick={handleSubmit} disabled={disabled}>
        My Button
      </button>
      <br />
      <br />
      {createContent()}
    </>
  );
};

export default Parent;

暂无
暂无

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

相关问题 如何在反应中使用钩子将数组从子组件传递到父组件 - How to pass an Array from Child component to Parent component using hooks in react 如何使用反应钩子将对象数组从子组件发送到父组件并存储在父对象中? - How to send array of objects data from child component to parent component and store in the parent object using react hooks? 如何使用 React Context 和 Hooks 从父组件重置子组件 - How to reset child component from parent using React Context and Hooks 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 如何使用 onClick 事件将 props 从子组件传递到父组件 React Hooks - How to use onClick event to pass the props from child component to parent component React Hooks 如何将子表单组件的输入值传递到其父组件的 state 以使用反应挂钩提交? - How to pass input values from a child Form component to the state of its parent component for submission with react hooks? 如何正确地将数组从父 React 类组件状态传递给使用钩子编写的子组件? - How to properly pass an array from a parent React class component state to a child component written with hooks? 如何使用反应钩子将 state 从子组件传递给父组件? - How to use react hooks to pass a state from child component to the parent component? 使用反应挂钩从父组件更新子组件的 state - update state of child component from parent component using react hooks 当我使用 React Hooks 将 boolean 从子组件传递到其父组件时出现了什么问题? - What is going wrong when I pass a boolean from a child to its parent component using React Hooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM