React 中何时使用花括号以及何时使用角度

2024-02-28

例如:

function Avatar(props) {
  return (
    <img className="Avatar"
      src={props.user.avatarUrl}
      alt={props.user.name}
    />
  );
}

function Comment(props) {
  return (
    <div className="Comment">
      <div className="UserInfo">
        <Avatar user={props.author} />
        <div className="UserInfo-name">
          {props.author.name}

为什么我不能使用 {Avatar(props.author)} 或 {Avatar() user={props.author}}?我知道我可以在大括号内使用函数,它是如何工作的?


我找到了答案:将函数声明为<Component/>,使其对 React 可见并允许您使用生命周期方法和挂钩。如果你将它用作函数:React 只能看到它返回的结果,但它的运行速度要快 45 倍。

感谢大家的帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React 中何时使用花括号以及何时使用角度 的相关文章

随机推荐