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

226
Views
React Hooks and Typescript- accessing a component's children and adding a prop and value

Say I have a material ui dialog component that renders children:

export interface DialogProps {
    open: boolean;
    title: string;
    children: React.ReactNode;
    onCancelClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
    saveButtonLabel?: string;
    onSaveClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
    permissionKey?: string
}
const Dialog = (props: DialogProps) => {
    const { children } = props;
    // takes a key and access level, returns true/false:
    const { checkPermissions } = useCurrentUser(); 

  <Dialog>
    <div>
       {children}
    </div>
    <DialogActions>
      <Button variant="outlined" onClick={props.handleCancelClick}>
          Cancel
      </Button>
      <Button
        variant="contained"
        onClick={props.handleSaveClick}
      >
         {props.saveButtonLabel ?? Save}
      </Button>
    </DialogActions>
  </Dialog>
}

I'm looking for a way to iterate over all the children passed to the dialog and check permissions and set a prop on certain children based on the result of the check. For example, if they have read-only I want to attach disabled to the children's prop so the field cannot be edited, rather than having the user of the dialog need to check permissions and set disabled manually. To have the user just pass a key to the dialog component and the dialog would check that key against a context provider function called checkPermissions and an access level (eg, Update).

Something like:

 React.useEffect(() => {
    if (props.permissionKey !== undefined) {
            if (!checkPermission(props.permissionKey, AccessLevel.Update)) {
                const childs = React.Children.toArray(children);
                React.Children.map(childs, (child, index) => {
                   // here set disabled on either all the children or even based on
                   // type. ie, if text field then disable. if another, then set some
                   // other prop appropriate to that type. 

                   if (typeof child is X) {
                        child.disabled = true;
                   }
                });
            }
        }
 },[])

Usage currently:

function MyComponent() {
    const [readOnly] = React.useState(!checkPermissions("somekey", AccessLevel.Update); 
     
   <MyDialog 
     open={true}
     title="My Dialog"
     permissionKey="someKey"
     ...
   >
      <TextField disabled={readOnly}>
        {data.someValue}
      </TextField>

      ... 
   </MyDialog>
} 

I've never seen this done. Is it possible to dynamically do this?

about 4 years ago · Juan Pablo Isaza
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!