79696481

Date: 2025-07-10 04:22:46
Score: 0.5
Natty:
Report link

It's completely valid — and sometimes a very good idea — to use plain JavaScript classes (like your GlobalParam) in a React project, especially for:

✅ When a Class is Fine

React components are used for rendering UI and handling UI-driven logic (with lifecycle/hooks). But not everything in a React app needs to be a component.

Your example:

export default class GlobalParam {     
static totalItems = 2;    
static getTotalData() {      
    return this.totalItems;    
    } 
} 

This is totally fine. It's essentially a singleton object with static properties/methods — perfect for shared config or utility logic that doesn’t involve React’s state/rendering lifecycle.

🤔 But Be Cautious

If the data inside GlobalParam is meant to be reactive (i.e., when it changes, your components should update), then a plain class won’t be sufficient, because React won’t know when to re-render.

Instead, you should use:

❓“How can I make a React data component without return?”

You don’t need to. If your component doesn’t render anything, it probably shouldn’t be a component.

But if you do want a component just for side effects (e.g., fetching, subscriptions), a common pattern is:

const DataLoader = () => {
   useEffect(() => {     
// Fetch data, subscribe, etc.  
 }, []);    
return null; // No UI 
}; 

Or make it a custom hook:

js

CopyEdit

function useGlobalData() { 
  const [data, setData] = useState(null);  
 useEffect(() => {    
 // fetch and set data 
  }, []);   
return data; 
} 

🔑 Summary

Let React handle the UI — let plain JavaScript handle logic when React isn't needed.

Reasons:
  • Blacklisted phrase (0.5): How can I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Srinivas Rao Gunja