简体   繁体   English

如何将功能作为道具从功能父组件传递给子组件

[英]How to pass function as props from functional parent component to child

Parent Component:父组件:

const initialValue_modalProps = [
    { show: false, response: "" }
  ];
const [modalProps, setModalProps] = useState(initialValue_modalProps)
const passedFunction = () => {
    setModalProps(modalProps => initialValue_modalProps);
  }
..
..
 <div>
        <Modal show={modalProps.show}
          response={modalProps.response}
          passedFunction={passedFunction}></Modal>
 </div>

Child Component:子组件:

export default function ModalComp(props) {
    const [modalOpen, setmodalOpen] = useState(true);
    console.log('modalOpen', modalOpen);
    if (props.show === false || modalOpen === false) {
        return null;
    }
    return (<Modal isOpen={props.show}>
        <ModalHeader>Deployment Status</ModalHeader>
        <ModalBody>{props.response}</ModalBody>
        <ModalFooter>
            <Button onClick={() => {
                setmodalOpen(modalOpen => false);
                props.passedFunction();
            }}>Close</Button>
        </ModalFooter>
    </Modal>)

}

Here I want to passedFunction function from Parent to child so that the Child component can execute it to reset the state in parent在这里,我想将 Parent 的函数传递给子组件,以便子组件可以执行它来重置父组件中的状态

You can take this as an reference with live example demo https://codesandbox.io/s/modal-6fvyx您可以将此作为实时示例演示https://codesandbox.io/s/modal-6fvyx的参考

function App() {
  const [status, setState] = React.useState(false);
  const [text, setText] = React.useState("");

  const handleClick = () => {
   setState(prevStatus => !prevStatus);
  };
  const handleChange = e => {
   setText(e.target.value);
  };


  return (
    <>
      <button onClick={handleClick}>Open photo entry dialog</button>
      <ChildComponent
        isOpen={status}
        text={text}
        handleChange={handleChange}
        handleClick={handleClick}
      />
    </>
  );
}

const ChildComponent = ({ isOpen, text, handleChange, handleClick }) => {
  return (
    <>
      {isOpen && (
        <Model
          status={isOpen}
          handleClick={handleClick}
          text={text}
          handleChange={handleChange}
        />
      )}
    </>
  );
};

You need to remove the parentheses behind passedFunction , because otherwise you are executing the function first and passing the result to the child afterwards.您需要删除passedFunction后面的括号,否则您将首先执行该函数,然后将结果传递给子进程。 Pass your function as it is via passedFunction={passedFunction} .通过passedFunction={passedFunction}传递你的函数。

const ParentComponent = () => {

  const initialModalProps = { ... };
  const [modalProps, setModalProps] = useState(initialModalProps);

  const passedFunction = () => {
    setModalProps(initialModalProps);
  }

  return (
    <div>
      <Modal
        show={modalProps.show}
        response={modalProps.response}
        passedFunction={passedFunction} />
    </div>
  );

};

Changed the child component to this.将子组件更改为此。 and its working及其工作

export default function ModalComp(props) {
    //const [modalOpen, setmodalOpen] = useState(true);
    if (props.show === false) {
        return null;
    }
    return (<Modal isOpen={props.show}>
        <ModalHeader>Deployment Status</ModalHeader>
        <ModalBody>{props.response}</ModalBody>
        <ModalFooter>
            <Button onClick={props.passedFunction}>Close</Button>
        </ModalFooter>
    </Modal>)

暂无
暂无

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

相关问题 当父组件和子组件也是功能组件时,如何将 function 作为道具传递? - How to pass a function as props when both Parent and Child component also a functional component? 如何将道具从子组件传递给父组件? - How to pass props from child component to parent? 如何将值从子功能组件传递给父类组件? - How to pass value from a child functional component to parent class component? 如何将道具从子组件传递到其父组件的父组件? - How to pass props from child component to its parent of parent? 我怎样才能将“用户名”作为道具从功能性无状态父级传递给功能性无状态子级,再传递给另一个有状态性子级? - How can I pass the `username` as props from a functional stateless parent to a functional stateless child to another stateful child? 如何在本机反应中将道具从父组件传递到子组件? - How to pass props from parent component to child component in react native? 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 如何在不使用ref方法的情况下从父项/传递道具到父项组件调用子函数? - How to call Child function from Parent / Pass Props to a Parent component without using ref methods? 如何在React中将props从父组件(函数)传递到子组件(CLASS) - How to pass props from parent component(function) to child component(CLASS) in React 如何将函数作为道具从一个功能组件传递到另一个? - How to pass function as props from one functional component to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM