With the introduction of Stateless functional components in react we have multiple ways to add helper methods to our component. What is the defined standard practice in regards to helper functions?
The helper-functionis not indended to be a general utility function. (which means it is only used for this specific component)
Place them inside the component-function?
export const myComponent = (props) => {
const myHelper = (value) => {
// Insert logic
};
return <div>{myHelper(props.mystate.value)}</div>;
};
Place them outside the function, but in the same file?
const myHelper = (value) => {
// Insert logic
};
export const myComponent = (props) => {
return <div>{myHelper(props.mystate.value)}</div>;
};
Or is there an alternative way that is commonly used?
Stateless Components The idea with a stateless functional component is that it is state-less and just a function. So what's great about this is that you are defining your component as a constant function that returns some data. In simple words, stateless functional components are just functions that return JSX.
A helper function is a function that performs part of the computation of another function. Helper functions are used to make your programs easier to read by giving descriptive names to computations. They also let you reuse computations, just as with functions in general.
Stateless components are those components which don't have any state at all, which means you can't use this. setState inside these components. It is like a normal function with no render method. It has no lifecycle, so it is not possible to use lifecycle methods such as componentDidMount and other hooks.
A functional(a.k.a. stateless) component is just a plain javascript function which takes props as an argument and returns a react element.
If you put the helper function inside the component function, everytime the component function gets executed, it will create a new instance of the helper function and after the function was executed the instance possibly gets garbage collected. So typically I would put it outside.
Of course there are exceptions, if you use an arrow function, you might need the lexical scope and therefor put it inside the component function, but if it's a pure function, I would say it should go outside.
If you intend to reuse the helper function, put it outside the stateless component function, either in same file or another, and export it:
export const myHelper = (value) => {
// Insert logic
};
Otherwise, it's up to you to decide - whatever makes sense to keep things organized.
You could do something like this, if you want to keep your stateless component pure:
const helpers = { helper: text => text.toUpperCase() }
const makeGreeter = helpers => props => <div>helpers.helper(props.greet)</div>;
const greeter = makeGreeter(helpers);
export default greeter;
This way, your component is a closure encapsulating its own helpers and it has predictable behavior.
As a note: If you modify helper
, and you could, because objects are mutable, greeter
wouldn't be a pure function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With