Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

when using onClick event with callback is not listen on React functional component in react

$
0
0

I have one flyout that listens to the onClick event to open to a new tab then close it. When I bypass the onClick function onClickOption as props, it is working but when I set it up as onClick inline callback function, the flyout is not close after clicking any <li>. anyone may know why?codebox

import './styles.css';import React, { FC, useRef, useState } from 'react';import Flyout from './Flyout';interface OptionPanelProps extends React.HTMLAttributes<HTMLElement> {}const noOp = (): void => void 0;export const OptionPanel: FC<OptionPanelProps> = () => {  const openNewTab = (): void => {    window?.open(`google.com`, '_blank');  };  return (<div><ul><li onClick={() => openNewTab()}>1</li><li onClick={() => openNewTab()}>2</li><li onClick={() => openNewTab()}>3</li></ul></div>  );};export const OptionFlyout: FC = () => {  const [isFlyoutOpen, toggleFlyout] = useState(false);  const anchorRef = useRef < HTMLButtonElement > null;  const closeFlyout = (): void => {    toggleFlyout(false);  };  return (<div className="recentSearch" data-tn="recentSearch-flyout"><button ref={anchorRef}>Option Menu</button><Flyout        anchor={anchorRef.current}        isOpen={isFlyoutOpen}        onDismiss={closeFlyout}>        {isFlyoutOpen && <OptionPanel onClick={() => closeFlyout()} />}</Flyout></div>  );};export default function App() {  return (<div className="App"><OptionFlyout /></div>  );}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>