Jason_Hough
Jason_Hough

Reputation: 484

Function props with functional component - is not a function (React + TypeScript)

I have a functional component with a function prop that does not work. So...

export interface IPanelProps {
  itemID; number;
  Open: boolean;
  onClose: () => void;
}


const FabricPanel: FC<IPanelProps> = ({ onClose, Open, itemID }) => {
  let _OnCancelClick = () => {
    onClose();
  }

  return (
    <Panel isOpen={Open}>
      <h2>{itemID} </h2>
      <DialogFooter>
        <DefaultButton text="Cancel" onClick={_OnCancelClick} />
        <PrimaryButton text="Save" />
      </DialogFooter>
    </Panel>
  )
}

Getting the error:

Uncaught TypeError: onClose is not a function

Open and Item ID props are working just fine, is it because I am calling a function prop? I have tried to put (props) instead and this did not work.

How can I fix this?

On the parent component:

   _renderPanelComponent = (props: any) => {
    const element : React.ReactElement<IPanelProps> = React.createElement(FabricPanel, assign({
      itemID : null,
      Open : false,
      OnClose : null
      

    }, props))
    ReactDom.render(element, this.panelPlaceHolder);
  }

  _showPanel = (itemID : number) => {
    this._renderPanelComponent({
      itemID,
      Open : true,
      OnClose : this._dismissPanel
      
      
    })
  }

  _dismissPanel = () =>{        
    this._renderPanelComponent({ isOpen: false });   
 }

When a button is pressed on SharePoint the arrow function Showpanel is executed and this works fine, but the onClose doesn't work.

Upvotes: 2

Views: 6569

Answers (1)

Jason_Hough
Jason_Hough

Reputation: 484

OnClose is spelt differently between the functional component and the parent comonent. So both are spelt onClose - sorted!

Upvotes: 1

Related Questions