如何在 React 中使用 useState Hook 调用父函数



理想的结果是在单击列表中的项目时显示字符串。

这是根组件。

const App = () => {
  const [click, setClick] = useState(null);
  const handleClick = name => {
    setClick(name);
  };
  return (
    <div>
      <Parent handleClick={handleClick} />
      {click && <p>{click} is clicked.</p>}
    </div>
  );
};

和父组件。

const Parent = ({ handleClick }) => (
  <div>
    <Child
      name="First Item"
      handleClick={handleClick("First Item is Clicked!")}
    />
    <Child
      name="Second Item"
      handleClick={handleClick("Second Item is Clicked!")}
    />
    <Child
      name="Third Item"
      handleClick={handleClick("Third Item is Clicked!")}
    />
  </div>
);

和子组件。

const Child = ({ name, handleClick }) => <li onClick={handleClick}>{name}</li>;

此外,代码沙盒链接。

我只是想知道为什么结果永远不会通过点击更改。

问题是您没有将函数传递给handleClick而是来自父级的评估值。您的代码必须是

import React from "react";
import Child from "./Child";
const Parent = ({ handleClick }) => (
  <div>
    <Child
      name="First Item"
      handleClick={() => handleClick("First Item is Clicked!")}
    />
    <Child
      name="Second Item"
      handleClick={() => handleClick("Second Item is Clicked!")}
    />
    <Child
      name="Third Item"
      handleClick={() => handleClick("Third Item is Clicked!")}
    />
  </div>
);
export default Parent;

工作演示

您正在调用 handleClick() 函数,而不是将函数传递给子组件。使用内联箭头函数包装调用。

注意:由于每当渲染父项时都会重新创建箭头函数,因此如果您有很多项目,或者由于其他原因不断渲染父项,这可能会导致性能问题。

const { useState } = React;
const App = () => {
  const [click, setClick] = useState(null);
  const handleClick = name => {
    setClick(name);
  };
  return (
    <div>
      <Parent handleClick={handleClick} />
      {click && <p>{click} is clicked.</p>}
    </div>
  );
};
const Parent = ({ handleClick }) => (
  <div>
    <Child
      name="First Item"
      handleClick={() => handleClick("First Item is Clicked!")}
    />
    <Child
      name="Second Item"
      handleClick={() => handleClick("Second Item is Clicked!")}
    />
    <Child
      name="Third Item"
      handleClick={() => handleClick("Third Item is Clicked!")}
    />
  </div>
);
const Child = ({ name, handleClick }) => <li onClick={handleClick}>{name}</li>;
ReactDOM.render(
  <App />,
  root
)
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>

另一种解决方案是将函数和文本传递给 Child 组件,并让它处理函数调用。在这种情况下,您可以使用useCallback来记忆函数:

const { useState, useCallback } = React;
const App = () => {
  const [click, setClick] = useState(null);
  const handleClick = name => {
    setClick(name);
  };
  return (
    <div>
      <Parent handleClick={handleClick} />
      {click && <p>{click} is clicked.</p>}
    </div>
  );
};
const Parent = ({ handleClick }) => (
  <div>
    <Child
      name="First Item"
      handleClick={handleClick} text="First Item is Clicked!"
    />
    <Child
      name="Second Item"
      handleClick={handleClick} text="Second Item is Clicked!"
    />
    <Child
      name="Third Item"
      handleClick={handleClick} text="Third Item is Clicked!"
    />
  </div>
);
const Child = ({ name, handleClick, text }) => {
  const handler = useCallback(() => handleClick(text), [handleClick, text]);
  
  return <li onClick={handler}>{name}</li>;
}
ReactDOM.render(
  <App />,
  root
)
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>

相关内容

  • 没有找到相关文章