reactjs - 从组件中的函数渲染值
问题描述
我在 React 中有一个这样的组件:
const myAge = () => {
var diff = Date.now() - new Date(1991, 2, 1);
var age = new Date(diff);
return age;
};
export const About = (props) => {
return (
<Container id={props.id}>
<p> My age is: {myAge}</p>
</Container>
)
}
我希望myAge
在标签中返回并呈现函数的结果p
,但没有呈现任何内容。我不知道我做错了什么,有什么建议吗?泰。
解决方案
您必须调用该函数,即{myAge()}
然后 thenew Date(diff)
不是字符串,因此您必须像这样强制转换它return String(age)
推荐阅读
- javascript - OpenCV.js 与 OpenCV Python 图像 imread rgb 的区别
- ios - Firebase iOS 电子邮件身份验证(无密码身份验证)可在 iPhone 上工作,但不能在浏览器上工作
- c - 错误:可变大小的对象可能未初始化。我不明白为什么?
- javascript - 如何使用 Vue.js 仅更改特定块的数据值?
- apache-flink - Flink 作业重启设置被忽略
- python - decompyle3 是否开始提供对 Python 3.9 的支持?
- assembly - 在汇编中将16位值扩展为32位形式以及关于U2代码的问题
- flutter - Flutter 构建设计
- applescript - 有没有办法使用 AppleScript 在 Google Chrome 中暂停 YouTube 视频?
- rust - 如何解决 Rust 中的“无法移动借来的值”(flatbuffers 示例)?