我有一个简单的 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/