Created
March 19, 2023 20:15
-
-
Save piotrlewandowski/fc2e8fc990dc6d0bce3ef61d92777a63 to your computer and use it in GitHub Desktop.
TestDome React Test 2 - portals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tooltip extends React.Component { | |
render() { | |
const { text, tooltipId } = this.props; | |
return ReactDOM.createPortal(text, document.getElementById(tooltipId)); | |
} | |
} | |
export class App extends React.Component { | |
state = { | |
text: '', | |
}; | |
onDocumentClick = (event) => { | |
if (event.target.tagName === 'BUTTON') { | |
this.setState({ text: event.target.textContent }); | |
} | |
}; | |
componentDidMount() { | |
document.addEventListener('click', this.onDocumentClick); | |
} | |
componentWillUnmount() { | |
document.removeEventListener('click', this.onDocumentClick); | |
} | |
render() { | |
return ( | |
<div> | |
{this.props.children} | |
<Tooltip text={this.state.text} tooltipId={this.props.tooltipId} /> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment