diff --git a/README.md b/README.md index 78e729de2..1fa1288bf 100644 --- a/README.md +++ b/README.md @@ -1001,45 +1001,37 @@ ReactDOM.render( Renders a ticker component. -- The ticker state is initially set to zero -- When the `Tick!` button is clicked, `timer` is incremented periodically at the given `interval` -- When the `Reset` button is clicked, the value of the timer is set to zero and the `setInterval` is cleared -- The `setInterval` is cleared once the desired `time` is reached -- `time` and `interval` are the required props +Use the `React.useState()` hook to initialize the `ticker` state variable to `0`. +Define two methods, `tick` and `reset`, that will periodically increment `timer` based on `interval` and reset `interval` respectively. +Return a `
` with two ` - -
- ); - } + return ( +
+ {this.state.ticker} + + +
+ ); } ``` @@ -1057,49 +1049,30 @@ ReactDOM.render(, document.getElementById('r Renders a toggle component. -Initialize `state.isToggleOn` to `false`, bind the `handleClick` method to the component's context. +Use the `React.useState()` to initialize the `isToggleOn` state variable to `false`. Use an object, `style`, to hold the styles for individual components and their states. -Create a method, `handleClick`, which uses `Component.prototype.setState` to change the component's `state.toggleOn`. -In the `render()` method, destructure `state` and `style`, create a ` - ); - } + return ( + + ); } ``` @@ -1117,67 +1090,48 @@ ReactDOM.render(, document.getElementById('root')); 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} +
+
+ ); } ``` diff --git a/data/snippet_data.json b/data/snippet_data.json index 648993aa6..3b19501ad 100644 --- a/data/snippet_data.json +++ b/data/snippet_data.json @@ -247,9 +247,9 @@ { "name": "Ticker.md", "title": "Ticker", - "text": "Renders a ticker component.\n\n- The ticker state is initially set to zero \n- When the `Tick!` button is clicked, `timer` is incremented periodically at the given `interval`\n- When the `Reset` button is clicked, the value of the timer is set to zero and the `setInterval` is cleared\n- The `setInterval` is cleared once the desired `time` is reached\n- `time` and `interval` are the required props\n\n", + "text": "Renders a ticker component.\n\nUse the `React.useState()` hook to initialize the `ticker` state variable to `0`.\nDefine two methods, `tick` and `reset`, that will periodically increment `timer` based on `interval` and reset `interval` respectively.\nReturn a `
` with two `\n\t\t\t\t\n\t\t\t
\n\t\t);\n\t}\n}\n```", + "```jsx\nfunction Ticker(props) {\n const [ticker, setTicker] = React.useState(0);\n let interval = null;\n\n const tick = () => {\n reset();\n interval = setInterval(() => {\n if (ticker < props.times) \n setTicker(ticker + 1);\n else \n clearInterval(interval);\n }, props.interval);\n }\n\n const reset = () => {\n setTicker(0);\n clearInterval(interval);\n }\n\n return (\n
\n {this.state.ticker}\n \n \n
\n );\n}\n```", "```jsx\nReactDOM.render(, document.getElementById('root'));\n```" ], "expertise": 1, @@ -263,9 +263,9 @@ { "name": "Toggle.md", "title": "Toggle", - "text": "Renders a toggle component.\n\nInitialize `state.isToggleOn` to `false`, bind the `handleClick` method to the component's context.\nUse an object, `style`, to hold the styles for individual components and their states.\nCreate a method, `handleClick`, which uses `Component.prototype.setState` to change the component's `state.toggleOn`.\nIn the `render()` method, destructure `state` and `style`, create a `\n );\n }\n}\n```", + "```jsx\nfunction Toggle(props) {\n const [isToggleOn, setIsToggleOn] = React.useState(false);\n style = {\n on: {\n backgroundColor: \"green\"\n },\n off: {\n backgroundColor: \"grey\"\n }\n };\n\n return (\n setIsToggleOn(!isToggleOn)}\n style={isToggleOn ? style.on : style.off}\n >\n {isToggleOn ? \"ON\" : \"OFF\"}\n \n );\n}\n```", "```jsx\nReactDOM.render(, document.getElementById('root'));\n```" ], "expertise": 0, @@ -279,9 +279,10 @@ { "name": "Tooltip.md", "title": "Tooltip", - "text": "Renders a tooltip component.\n\nSet the `state` of the component to `show: false` initially, define an object, `style`, to hold the styles for individual components and their states.\nCreate a method, `toggleTooltip`, which uses `this.setState` to change the state's `show` property from `true` to `false` and vice versa.\nBind `showTooltip` and `hideTooltip` to the component's context with the respective values of `true` and `false`.\nIn 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.\n \n", + "text": "Renders a tooltip component.\n\nUse the `React.useState()` hook to create the `show` variable and initialize it to `false`.\nReturn a `
` element that contains the `
` 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 \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} +
+
+ ); } ```