Hyperbola
Hyperbola

Reputation: 528

What should go inside a functional component

While writing a functional component in React, where do you keep the constants and utility functions? Inside the react component or outside?

import React from 'react';

// A global constant
const EARTH_RADIUS = 3959;

// A utility function
const volume = () => {
  return (4/3) * (22/7) * Math.pow(EARTH_RADIUS, 3);
}

const MyComponent = () => {
  return (
    <div>
      <h1>Earth's radius: {EARTH_RADIUS}</h1>
      <p>{volume()}</p>
    </div>
  )
}

Should EARTH_RADIUS and volume be inside MyComponent? Is there any rule when to include or exclude, what's the best practice for this?

Upvotes: 0

Views: 43

Answers (1)

Saul Ramirez
Saul Ramirez

Reputation: 426

A good way to do it is creating a folder called config or utils to put this kind of code in order to reused it in others components.

I hope this help you

Upvotes: 1

Related Questions