Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Alternative to extending a functional component

I have the following component where within the useEffect, I am calling some data reading related functions meant to happen once on load.

The problem is, some of the prop data are not available at this stage (still undefined) like the prodData and index.
They are only available when I get into the Nested components like <NestedComponent1 />.

I wish to move this logic into the nested components which will resolve this issue.

But I do not want to repeat these code inside the useEffect for each component. Instead looking to write these 7 lines once maybe in a function
and just call it with the 3 NestedComponents.

Issue is that there is a higher order function wrapping here plus all the values like prodData and index is coming from Redux store.

I can't just move all these logic inside useEffect into a normal JS function and instead need a functional component for this.

And if I make a functional component to perform these operations, I can't call it in the useEffect for each of the NestedComponents.

Cos this is not valid syntax.

React.useEffect(() => {
    <NewlyCreatedComponentWithReadingFunctionality />
}, []);

Thus my query is, is there a way I could write a functional component which has the data reading logic inside its useEffect. And then extend this functional component for each of the functional components so that the useEffect would just fire when each of these NestedComponents are called?

Doesn't seem to be possible to do this thus looking for alternatives.

This is the existing component where some of these prop values are undefined at this stage.

const MyComponent = ({
  prodData,
  index,
  country,
  highOrder: {
    AHigherOrderComponent,
  },
}) => {

  // this is the logic which I am looking to write once and be 
  // repeatable for all the NestedComponent{1,2,3}s below.
  React.useEffect(() => {
    const [, code] = country.split('-');
    const sampleData = prodData[index].sampleData = sampleData;
    const period = prodData[index].period = period;
    const indication = prodData[index].indication = indication;

    AHigherOrderComponent(someReadDataFunction(code, sampleData));
    AHigherOrderComponent(someReadDataFunction(code, period);
    AHigherOrderComponent(someReadDataFunction(code, indication);
  }, []);

  return (
      {/* other logics not relevant */}
      <div>
        <div>
            <NestedComponent1 />
            <NestedComponent2 />
            <NestedComponent3 />
        </div>
      </div>
  );
};
export default connect( // redux connect
  ({
    country,
    prodData,
    index,
  }) => ({
    country,
    prodData,
    index,
  })
)(withHighOrder(MyComponent));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

React components implement a pattern called composition. There are a few ways to share state between parts of your React application but whenever you have to remember some global state and offer some shared functionality, I would try and manage that logic inside a context provider.

I would try the following:

  • Wrap all your mentioned components inside a context provider component
  • Offer the someReadDataFunction as a callback function as part of the context
  • Within your provider, manage react state, e.g. functionHasBeenCalled that remembers if someReadDataFunction has been called already
  • Set functionHasBeenCalled to true inside someReadDataFunction
  • Call someReadDataFunction inside your components within a useEffect based on the props data

This way, your application globally remembers if the function has been executed already but you can still use the latest data within your useEffect within your components to call someReadDataFunction.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!