facebookarchive / react-meteor

React rendering for Meteor apps
948 stars 113 forks source link

How to access helper functions #101

Closed tgoldenberg closed 9 years ago

tgoldenberg commented 9 years ago

This seems like a really basic question, but how do we access helper functions from within a React class with this Meteor integration? I tried putting a function in a helper file but it isn't getting read by the .jsx file. On the internet I saw suggestions to try to get Meteor to use require('../file-name.js') syntax, but that seems kind of hackish. Shouldn't there be an easy solution to this? It isn't absolutely necessary, but feels good to refactor out code :)

froatsnook commented 9 years ago

You could always do something like:

// home.js
Template.home.helpers({
    test: function() { return true; }
});

// HomeContent.jsx
HomeContent = ReactMeteor.createClass({
    render: function() {
        var isTest = Template.home.__helpers[" test"]();
    }
});

__helpers has two underscores and you have to put a space in front of the helper name to access it. Also, the helper shouldn't use the value of this unless you call it.

Is it really necessary to refactor it into a helper? Or could you just make a function somewhere?

// Test.js
Test = function() { return true; }

// HomeContent.jsx
HomeContent = ReactMeteor.createClass({
    render: function() {
        var isTest = Test();
    }
});
tgoldenberg commented 9 years ago

Thanks!