[英]Meteor and React: Helpers
I've been looking into moving from Blaze to React as the frontend for my Meteor apps.我一直在考虑从 Blaze 迁移到 React 作为我的 Meteor 应用程序的前端。 I can't seem to find is equivalent of helpers in Blaze which use UI.registerHelper
我似乎无法找到相当于在大火佣工其中使用UI.registerHelper
UI.registerHelper
in Blaze allows you to setup globally accessible helpers that can be used across any template. Blaze 中的UI.registerHelper
允许您设置可在任何模板中使用的全局可访问助手。
A typical example might be a helper that transforms an ISO Date into a human readable date format.一个典型的例子可能是一个将 ISO 日期转换为人类可读日期格式的助手。
From what I can see, React Classes can have their own properties which can be functions that perform helper-like tasks.据我所知,React 类可以有自己的属性,这些属性可以是执行类似辅助任务的函数。 But what I can't see is how you create them on a global level.但是我看不到的是您如何在全球范围内创建它们。
How is this achieved in when using React in Meteor?在 Meteor 中使用 React 时这是如何实现的?
you don't need the reactivity of a helper, since a change in the DB will trigger a change in the meteor mixin, thus trigging a change in your render()
.您不需要助手的反应性,因为数据库中的更改将触发流星混合中的更改,从而触发render()
的更改。 So, all you need is a global function.所以,你所需要的只是一个全局函数。
file: formatDate.jsx
module.exports = function(dateStr) {
return new Date(dateStr);
}
then at the top of your component:然后在组件的顶部:
let formatDate = require('./formatDate.jsx')
I haven't used react before.我以前没有使用过反应。 although in meteor, I think setup globally accessible helpers is not a good way to solve this problem, because it may pollute app namespace.虽然在meteor中,我认为设置全局可访问的helpers并不是解决这个问题的好方法,因为它可能会污染应用程序命名空间。
So I always use some module loader, like Require.js , to define your useful helpers function as a util
module and require it in your other code.所以我总是使用一些模块加载器,比如Require.js ,将你的有用助手函数定义为一个util
模块,并在你的其他代码中使用它。
I wish this general solution could help you :-)我希望这个通用解决方案可以帮助你:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.