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

92
Views
Is it best way to handle all state in page with React

I used to use Elm language. It should have an state on the page (such as normal, loading, error) and I can write in Elm language like this

type Action = Choose | Edit | Confirm

type ModalType = Address | Payment | Tax

type State
   = Normal
   | PopupModal ModalType Action
   | Error String


// and use like this
Normal (normal state)
PopupModal Address Choose (Popupmodal state)
Error "something went wrong" (Error state)

It's mean If you want to use a PopupModal state, you should send Action and ModalType as a parameter. or if you want to Normal state, you don't send anything.

So, I want to use this concept in React. It has 2 state(Normal and Popup modal) and "Popup modal" state has 3 state(choose, edit, confirm)

Is it the best way to use this concept in React? If it is best, how can I write it in React?

for now, I write it like this

const ACTION = Object.freeze({
  CHOOSE: "CHOOSE",
  EDIT: "EDIT",
  CONFIRM: "CONFIRM",
});

const MODAL_TYPE = Object.freeze({
  ADDRESS: "ADDRESS",
  PAYMENT: "PAYMENT",
  TAX: "TAX",
});

const STATE = Object.freeze({
  NORMAL: "NORMAL",
  MODAL: (type, action) => {
    switch (type) {
      case MODAL_TYPE.ADDRESS:
        return <Address action={action} />
      case MODAL_TYPE.PAYMENT:
        return <Payment action={action} />
      case MODAL_TYPE.TAX:
        return <Tax action={action} />
      default:
        break;
    }
  },
});

const render = (state) => (type, action) => {
  switch (state) {
    case STATE.NORMAL:
      return <Normal />;
    case STATE.MODAL:
      return state(type, action);
    default:
      break;
  }
};

// Normal state
render(STATE.NORMAL)()

// Popup modal state
render(STATE.MODAL)(MODAL_TYPE.PAYMENT, ACTION.CHOOSE)
render(STATE.MODAL)(MODAL_TYPE.TAX, ACTION.CHOOSE)

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

0

there is some options:

a) Use a typescript interface which exactly hold typed data in useState hook.

export enum UserStatus {
  LOADING = "loading",
  SUCCESS = "success",
  ERROR = "error",
}

  const [selectedCategories, setSelectedCategories] = useState<States>([]);

b) After read yours post - the recognize of variants - should be reliable in this context enter image description here


c) Prepare generic Modal component - which hold some general properties (title, desc) - with setting variable - and based on that setting it render the case based on that variable:

  {props.setVar == 'address' && <Address />}
  {props.setVar == 'payment' && <Payment />}
  {props.setVar == 'tax' && <Tax />}
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!