From 0eeac148c67ffd8f32a0c5437bc9bb7da71e9c6e Mon Sep 17 00:00:00 2001 From: komv8i Date: Thu, 7 Feb 2019 14:23:46 -0500 Subject: [PATCH 1/2] change collapse component to hooks implementation --- snippets/Collapse.md | 72 ++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/snippets/Collapse.md b/snippets/Collapse.md index 4da570282..2cf9a25a7 100644 --- a/snippets/Collapse.md +++ b/snippets/Collapse.md @@ -5,52 +5,46 @@ Renders a component with collapsible content. Use the value of the `collapsed` prop to determine the initial state of the content (collapsed/expanded). Set the `state` of the component to the value of the `collapsed` prop (cast to a boolean value) and bind the `toggleCollapse` method to the component's context. Use an object, `style`, to hold the styles for individual components and their states. -Create a method, `toggleCollapse`, which uses `Component.prototype.setState` to change the component's `state` from collapsed to expanded and vice versa. +Create a method, `toggleCollapse` to change the component's `state` from collapsed to expanded and vice versa. In the `render()` method, use a `
` to wrap both the ` -
- {this.props.children} -
+}; + +function Collapse(props) { + const [isCollapsed, setCollapsed] = useState(props.collapsed); + + const toggleCollapse = () => { + setCollapsed(!isCollapsed); + }; + + return ( +
+ +
+ {props.children}
- ); - } +
+ ); } ``` From 6831b872a2c292ec1d8a8465ba890501f4bec3ba Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 12 Feb 2019 20:24:50 +0200 Subject: [PATCH 2/2] Update Collapse.md --- snippets/Collapse.md | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/snippets/Collapse.md b/snippets/Collapse.md index 2cf9a25a7..04dd27182 100644 --- a/snippets/Collapse.md +++ b/snippets/Collapse.md @@ -2,38 +2,35 @@ Renders a component with collapsible content. -Use the value of the `collapsed` prop to determine the initial state of the content (collapsed/expanded). -Set the `state` of the component to the value of the `collapsed` prop (cast to a boolean value) and bind the `toggleCollapse` method to the component's context. +Use the `React.setState()` hook to create the `isCollapsed` state variable with an initial value of `props.collapsed`. Use an object, `style`, to hold the styles for individual components and their states. -Create a method, `toggleCollapse` to change the component's `state` from collapsed to expanded and vice versa. -In the `render()` method, use a `
` to wrap both the `