Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access static method when using connect & HoC?

To be able to fetch data for server-side rendering, it is recommended to setup a static method fetchData which will populate the Redux store through async actions.

When using connect the component is wrapped and is only accessible through WrappedComponent. However static methods are not accessible. The problem is further accentuated when using other HoC, such as reducerInjector. What is the recommended approach?

like image 946
Kevin Farrugia Avatar asked Jun 18 '26 16:06

Kevin Farrugia


1 Answers

From the react-redux docs you see that connect uses hoistNonReactStatics (and if you look in the source code, you'll find it in connectAdvanced which is then extended by connect).

So if you're using connect, you should still have access to your own static methods. However, if you're using other HoCs, the static methods might not be copied over.

Another option is to make your static method a function

export default myFunction = () => { /* do stuff */ }

and import it where you need to use it.

like image 91
Daniel Andrei Avatar answered Jun 21 '26 15:06

Daniel Andrei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!