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

98
Views
useState with an object

There are two separate objects coming from the backend. I want to create two different objects using the same state. Then I want to call and print them. How can I do it?

PolicyContext

import React, { createContext, useMemo, useState } from 'react';

export const PolicyContext = createContext(undefined);

const PolicyContextProvider = (props) => {
  const [policyData, setPolicyData] = useState({
    activiesPolicy: '',
    passivePolicy: ''
  });

  return (
    <PolicyContext.Provider
      value={useMemo(() => {
        return ({
          policyData,
          setPolicyData,
        });
      }, [{ policyData, setPolicyData }])}
    >
      {props.children}
    </PolicyContext.Provider >
  );
};

export default PolicyContextProvider;

home.js

  const activiesPolicyExampleReq = {
    nationalId: '59104492600',
  };

  const passivePolicyExampleReq = {
    nationalId: '59104492600',
  };

  const { policyData, setPolicyData } = useContext(PolicyContext);
  useEffect(() => {
    if (Object.keys(policyData.activiesPolicy).length === 0) {
      PolicyService.getActivePolicies(activiesPolicyExampleReq).subscribe(
        (response) => {
          setPolicyData(response);
        }
      );
    }
  }, []);

  useEffect(() => {
    if (Object.keys(policyData.passivePolicy).length === 0) {
      PolicyService.getPassivePolicies(passivePolicyExampleReq).subscribe(
        (response) => {
          setPolicyData(response);
        }
      );
    }
  }, []);

I think my queries are overwhelming each other.

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

0

It looks like you want to assign a response to a specific key in your state object. You could do that by spreading the existing state and modifying only part of it when you call setState hook.

Given you have a state like this:

  const [policyData, setPolicyData] = useState({
    activiesPolicy: '',
    passivePolicy: ''
  });

you probably want to do something like this:

  useEffect(() => {
    if (Object.keys(policyData.activiesPolicy).length === 0) {
      PolicyService.getActivePolicies(activiesPolicyExampleReq).subscribe(
        (response) => {
          setPolicyData((prevState) => ({...prevState, activiesPolicy: response});
        }
      );
    }
  }, []);

  useEffect(() => {
    if (Object.keys(policyData.passivePolicy).length === 0) {
      PolicyService.getPassivePolicies(passivePolicyExampleReq).subscribe(
        (response) => {
          setPolicyData((prevState) => ({...prevState, passivePolicy: response});
        }
      );
    }
  }, []);
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!