Javascript 从NPM包向函数传递道具
我有一个简单的React应用程序,我想在其中使用Javascript 从NPM包向函数传递道具,javascript,reactjs,npm,Javascript,Reactjs,Npm,我有一个简单的React应用程序,我想在其中使用 react timeagonpm模块 我看了文档,其中有一个我想要使用的函数 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'
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.
这在文档中显示为道具,但如何将参数作为道具传递给函数
<TimeAgo date={this.state.time} formatter={/*how can i pass in the arguments to the function */} live={true}/>
我在这里调用了组件,但我不知道如何将正确的道具传递给组件?您为
格式化程序
道具提供了一个函数,react timeago
将在内部使用
示例
<TimeAgo
date={this.state.time}
formatter={(value, unit, suffix) => `${value} ${unit} ${suffix}`}
live={true}
/>
`${value}${unit}${suffix}`}
live={true}
/>
文档中说,格式化程序
接受一个函数,因此您要么给它一个预定义函数,要么给它一个您自己定义的函数
重要的是,这是一个回调函数,因此不需要调用或发送参数。
但是我要在模板文本中填充值吗?或者我如何传递参数?@baileyaldwin当react timeago
调用函数时,这些参数将被赋予函数,这正是您想要命名的。(foo,bar,baz)=>${foo}${bar}${baz}
也可以工作,您当然可以引用当前范围内的其他变量。啊,就像普通函数一样?因此,例如'formatter={(value=0,unit='minute',suffix='ago')=>${value}${unit}${suffix}
}`
import frenchStrings from 'react-timeago/lib/language-strings/fr'
import buildFormatter from 'react-timeago/lib/formatters/buildFormatter'