` that will be the tooltip and the `children` passed to the component.\nHandle the `onMouseEnter` and `onMouseLeave` methods, by altering the value of the `show` variable.\n \n",
"codeBlocks": [
- "```jsx\nclass Tooltip extends React.Component {\n constructor(props) {\n super(props);\n this.state = {\n show: false\n };\n this.style = {\n tooltip: {\n position: 'relative',\n backgroundColor: \"rgba(0,0,0,0.7)\",\n color: \"white\",\n visibility: \"hidden\",\n width: \"fit-content\",\n padding: 5,\n borderRadius: 5\n },\n tooltipArrow: {\n position: 'absolute',\n top: '100%',\n left: '50%',\n borderWidth: 5,\n borderStyle: 'solid',\n borderColor: \"rgba(0,0,0,0.7) transparent transparent\",\n },\n visible: {\n visibility: \"visible\"\n },\n };\n this.showTooltip = this.toggleTooltip.bind(this, true);\n this.hideTooltip = this.toggleTooltip.bind(this, false);\n }\n\n toggleTooltip = tooltipState => {\n this.setState({\n show: tooltipState\n });\n };\n\n render() {\n const { children, text, ...rest } = this.props;\n const { show } = this.state;\n const { visible, tooltip, tooltipArrow } = this.style;\n const showTooltip = show ? visible : {};\n return (\n
\n
\n {text}\n \n
\n
\n {children}\n
\n
\n );\n }\n}\n```",
+ "```css\n.tooltip {\n position: relative;\n background: rgba(0, 0, 0, 0.7);\n color: white;\n visibility: hidden;\n padding: 5px;\n border-radius: 5px;\n}\n.tooltip-arrow {\n position: absolute;\n top: 100%;\n left: 50%;\n border-width: 5px;\n border-style: solid;\n border-color: rgba(0, 0, 0, 0.7) transparent transparent;\n}\n```",
+ "```jsx\nfunction Tooltip({ children, text, ...rest }) {\n const [show, setShow] = React.useState(false);\n\n return (\n
\n
\n {text}\n \n
\n
setShow(true)}\n onMouseLeave={() => setShow(false)}\n >\n {children}\n
\n
\n );\n}\n```",
"```jsx\n ReactDOM.render(\n
\n Hover me! \n ,\n document.getElementById('root')\n );\n```"
],
"expertise": 1,
diff --git a/snippets/Tooltip.md b/snippets/Tooltip.md
index d99d1bca0..cf9e8285d 100644
--- a/snippets/Tooltip.md
+++ b/snippets/Tooltip.md
@@ -2,67 +2,47 @@
Renders a tooltip component.
-Set the `state` of the component to `show: false` initially, define an object, `style`, to hold the styles for individual components and their states.
-Create a method, `toggleTooltip`, which uses `this.setState` to change the state's `show` property from `true` to `false` and vice versa.
-Bind `showTooltip` and `hideTooltip` to the component's context with the respective values of `true` and `false`.
-In the `render()` method, compute if the tooltip should be shown or hidden, render the content of the tooltip and bind the `onMouseEnter` and `onMouseLeave` events to `showTooltip` and `hideTooltip` respectively.
+Use the `React.useState()` hook to create the `show` variable and initialize it to `false`.
+Return a `
` element that contains the `
` that will be the tooltip and the `children` passed to the component.
+Handle the `onMouseEnter` and `onMouseLeave` methods, by altering the value of the `show` variable.
+```css
+.tooltip {
+ position: relative;
+ background: rgba(0, 0, 0, 0.7);
+ color: white;
+ visibility: hidden;
+ padding: 5px;
+ border-radius: 5px;
+}
+.tooltip-arrow {
+ position: absolute;
+ top: 100%;
+ left: 50%;
+ border-width: 5px;
+ border-style: solid;
+ border-color: rgba(0, 0, 0, 0.7) transparent transparent;
+}
+```
```jsx
-class Tooltip extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- show: false
- };
- this.style = {
- tooltip: {
- position: 'relative',
- backgroundColor: "rgba(0,0,0,0.7)",
- color: "white",
- visibility: "hidden",
- width: "fit-content",
- padding: 5,
- borderRadius: 5
- },
- tooltipArrow: {
- position: 'absolute',
- top: '100%',
- left: '50%',
- borderWidth: 5,
- borderStyle: 'solid',
- borderColor: "rgba(0,0,0,0.7) transparent transparent",
- },
- visible: {
- visibility: "visible"
- },
- };
- this.showTooltip = this.toggleTooltip.bind(this, true);
- this.hideTooltip = this.toggleTooltip.bind(this, false);
- }
+function Tooltip({ children, text, ...rest }) {
+ const [show, setShow] = React.useState(false);
- toggleTooltip = tooltipState => {
- this.setState({
- show: tooltipState
- });
- };
-
- render() {
- const { children, text, ...rest } = this.props;
- const { show } = this.state;
- const { visible, tooltip, tooltipArrow } = this.style;
- const showTooltip = show ? visible : {};
- return (
-
-
- {text}
-
-
-
- {children}
-
+ return (
+
+
+ {text}
+
- );
- }
+
setShow(true)}
+ onMouseLeave={() => setShow(false)}
+ >
+ {children}
+
+
+ );
}
```