DEV Community

Discussion on: Why Is This An "Anti-Pattern" in React???

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

As for functional components, I freely admit that, in my "side" dev, I've still been writing a lotta "old school" class components. And this solution was built with that in mind. But I'm not entirely understanding why it simply "wouldn't work" with functional components? Granted, I'm not trying this in an IDE right now, but isn't the following valid?

import components from './components';
import React from 'react';

function DoSomething() {
   console.log(components.ContentArea.state.reduxSucks);
   return <div>Did it.</div>;
}

components.DoSomething = DoSomething;

Of course, you'd have to ensure that your app is designed in such a way that DoSomething() can never be invoked until after the <ContentArea> component's been mounted.