reactjs - 在 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}}?我知道我可以在花括号中使用函数,它是如何工作的?
解决方案
我找到了答案:将 function 声明为<Component/>
,使其对 React 可见,并允许您使用生命周期方法和钩子。如果你将它用作函数:React 只看到从它返回的结果,但它的工作速度快了约 45 倍。
感谢大家的帮助!
推荐阅读
- github - 如何在 github 拉取请求中显示评论应用程序 url
- flutter - Flutter bloc 状态无法正确更新
- java - 如何将 thymeleaf HTML 页面中的数据发送到 MVC spring boot 控制器?
- html - 如何为每周调度程序 UI 设置跨度元素的样式
- azure - 错误:将 Function 部署到 Azure 后,无法加载文件或程序集“System.ServiceModel,版本 = 4.0.0.0”
- c - GCC 会编译,但 clang 不会
- node.js - NPM 没有显示最新版本的 json-parse-better-errors
- php - 如何使用 CI 和 Morris 在 1 个视图中制作视图表和图形
- python - Ctrl+C 不让我退出服务器 | 蟒蛇
- metasploit - 如何使用 metasploit 开始和活动