javascript - 将 props 传递给 NPM 包中的函数

标签 javascript reactjs npm

我有一个简单的 React 应用程序,我想在其中使用 react-timeago npm 模块

我查看了文档,其中有一个我想使用的函数

formatter (optional)
A function that takes four arguments:

value : An integer value, already rounded off
unit : A string representing the unit in english. This could be one of:
'second'
'minute'
'hour'
'day'
'week'
'month'
'year'
suffix : A string. This can be one of
'ago'
'from now'
epochSeconds: The result of Date.now() or the result of a custom now prop.
nextFormatter: A function that takes no arguments and gives you the result of the defaultFormatter using the same arguments above.

这在文档中显示为 Prop ,但是我如何将参数作为 Prop 传递给函数?

     <TimeAgo date={this.state.time} formatter={/*how can i pass in the arguments to the function */} live={true}/>

我在这里调用了该组件,但我对如何将正确的 props 传递给组件有疑问?

最佳答案

您为 formatter 属性提供一个函数,react-timeago 将在内部使用。

示例

<TimeAgo
  date={this.state.time}
  formatter={(value, unit, suffix) => `${value} ${unit} ${suffix}`}
  live={true}
/>

关于javascript - 将 props 传递给 NPM 包中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54735923/

相关文章:

javascript - 更改子元素的样式以在悬停父页面元素 36 次时显示唯一的 div

javascript - 正则表达式匹配字符串格式为 @ 或 # 后跟点分隔的字符串,不包含空格

reactjs - 检查渲染方法

css - 无法让我的 React 应用填满屏幕

macos - 在没有 sudo 的情况下安装 Cordova 和 Ionic

javascript - 当我已经安装了 lodash 时,我还需要 n_ 吗?

javascript - 在 div 位置渲染 cytoscape 图

javascript - NodeJS - 帖子过期

javascript - 如何将material-ui时间选择器更改为24小时格式

node.js - LevelDOWN : NODE_MODULE_VERSION error when testing, 但未运行