简体   繁体   English

通过 React 功能组件将 function 从父级传递给子级

[英]Passing down function from parent to child through React functional component

I am trying to pass down the functions setDir and handleSort() down from SearchAppointments (parent) to Hooks (child), but I keep getting errors saying that they are not functions.我正在尝试将函数setDirhandleSort()SearchAppointments (父)传递给Hooks (子),但我不断收到错误消息,说它们不是函数。

I tried to debug it by looking the typeof handleSort in the useEffect hook of the child component, though it console.logged two statements: 1) underfined 2) function.我试图通过查看子组件的useEffect钩子中的typeof handleSort来调试它,尽管它在控制台记录了两个语句:1)underfined 2)function。 Not sure what is wrong.不知道出了什么问题。

const SearchAppointments = React.memo(() => {
  const [orderDir, setDir] = useState("");

  const handleSort = (e) => {
    let value = e.target.value;
    setDir(value);
    let order;
    let filterData = data;
    if (orderDir === 'asc') {
      order = 1;
    } else {
      order = -1;
    }
  };

  return (
    <>
      <div>
        <Hooks handleSort={handleSort} setDir={setDir} />
      </div>
    </>
  );
});

const Hooks = React.memo(({ handleSort, setDir }) => {
  useEffect(() => {
    console.log(typeof handleSort);
  }, []);

  return (
    <div>
      <div>
        <button type="button" onClick={() => setDir("success")}>
          Set Dir
        </button>
        <button type="button" value='asc' onClick={handleSort}>
          Handle Sort (Asc)
        </button>
        <button type="button" value='dsc' onClick={handleSort}>
          Handle Sort (Dsc)
        </button>
      </div>
    </div>
  );
});

try and use memo like this:尝试像这样使用memo

const comparator = (previous, next) => {
    if (something) {
       return true
    }
    return false
}
const Hooks = React.memo(({ handleSort, setDir }) => {
  useEffect(() => {
    console.log(typeof handleSort);
  }, []);

  return (
    <div>
      <div>
        <button type="button" onClick={() => setDir("success")}>
          Set Dir
        </button>
        <button type="button" value='asc' onClick={handleSort}>
          Handle Sort (Asc)
        </button>
        <button type="button" value='dsc' onClick={handleSort}>
          Handle Sort (Dsc)
        </button>
      </div>
    </div>
  );
}, comparator);

define a function and pass that as the second argument.定义一个 function 并将其作为第二个参数传递。 memo function (I've called it comparator , then returns true or false depending on when you want the component to update. think of it as the new shouldComponentUpdate in the lifecycle methods. but use with care as your application might not update if you use carelessly memo comparator (我称它为 compare ,然后根据您希望组件更新的时间返回truefalse 。将其视为生命周期方法中的新shouldComponentUpdate 。但请小心使用,因为如果您使用,您的应用程序可能不会更新不小心

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

相关问题 通过函数参数将属性从动态子组件传递到父功能组件 react-native - Passing properties from a dynamic Child component to a Parent functional component through a functions parameters react-native 反应 function 不会从功能组件传递到功能组件 - React function not passing down from functional to functional component 从其子项触发 React 父功能组件中的 function - Triggering a function in a React parent functional component from its child 将用户输入从子功能组件传递到父功能组件 - Passing user input from child functional component to parent functional component 在 React 中将功能组件从子组件传递给父组件 - Pass functional component from child to parent in React 为什么将 function 从父 Class 组件传递给子功能组件不起作用? - Why passing a function from a parent Class Component to a child Functional Component doesn't work? 将数据从子级传递到父级组件-反应-通过回调函数 - passing data from child to parent component - react - via callback function 在react-redux中将函数从子组件传递给父组件 - Passing function from child to parent component in react-redux 在 React 中通过 function 从父组件传递到子组件? - Passing function via props from Parent to Child component in React? 将数据从功能组件传递到父类组件反应原生 - Passing data from a functional component to a parent class component react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM