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

162
Views
ReactJS + Typescript: Set component state with context property value without triggering a rerender

I am relatively new to ReactJS and I'm building an app with a Context. At some point I need to get one of the values from the Context, make it available for editing and only after it's submitted change it's value in the Context (to avoid refreshing all the other components). I thought of doing something like this:

export default class AComponent extends Component<any, { property?: Property }> {
  public constructor(props: any) {
    super(props);
  }

  public shouldComponentUpdate(nextProps: any, nextState: { property?: Property }) {
    return nextState.property !== this.state.property;
  }

  public render(): JSX.Element {
    return (
      <AContext.Consumer>
        {(data) => {
          // ...
          this.setState({ ...this.state, property: data.property });
          // ...    
        }}
      </AContext.Consumer>
    );
  }
}

But even with the shouldComponentUpdate check, it still refreshes 4 times! Without it it produces an error (recursion limit reached or something like that).

The question is: What's the correct way of working with a context property without changing it until the moment you actually want to?

I wanted to get the Context property inside the constructor but I have found that this form is deprecated:

constructor(props: any, context: AContext) {
  super(props, context)
}

Is there any other way?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

not only can property add to context but you can also add methods to them.

  • Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language

  • if you want to change data from different components, I prefer to use state management like Redux

    happy codeing

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!