首页 > 解决方案 > 在 React 中何时使用花括号以及何时使用角度

问题描述

例如:

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}}?我知道我可以在花括号中使用函数,它是如何工作的?

标签: reactjsreact-propshigh-order-component

解决方案


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

感谢大家的帮助!


推荐阅读