Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helpers in stateless functional components

Tags:

reactjs

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?

like image 531
Etse Avatar asked Jan 31 '16 09:01

Etse


People also ask

Can stateless components have functions?

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.

What are helper functions?

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.

What is a stateless functional component?

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.

Which component is also known as a stateless component?

A functional(a.k.a. stateless) component is just a plain javascript function which takes props as an argument and returns a react element.


3 Answers

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.

like image 169
gwildu Avatar answered Oct 11 '22 00:10

gwildu


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.

like image 26
sbecker Avatar answered Oct 10 '22 23:10

sbecker


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.

like image 1
Mario Gil Avatar answered Oct 11 '22 00:10

Mario Gil