简体   繁体   中英

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.

Any ideas at all?

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 doesnt work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM