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.
TypeErrorhappens at runtime - it looks likeonCloseisn't defined, or is not a function. Try debugging whatonCloseis at run time (i.econsole.log(onClose))itemID;should beitemID:. although everything else looks fine. can uconsole.log(typeof onClose)?<FabricPanel />must passonCloseproperty which is afunction. For example,<FabricPanel onClose={() => {}} />