diff --git a/snippets/TreeView.md b/snippets/TreeView.md index 955817b98..0a48dd5e2 100644 --- a/snippets/TreeView.md +++ b/snippets/TreeView.md @@ -2,13 +2,12 @@ Renders a tree view of a JSON object or array with collapsible content. -Use `defaultProps` to set the default values of certain props. +Use object destructuring to set defaults for certain props. Use the value of the `toggled` prop to determine the initial state of the content (collapsed/expanded). -Set the `state` of the component to the value of the `toggled` prop and bind the `toggle` method to the component's context. -Create a method, `toggle`, which uses `Component.prototype.setState` to change the component's `state` from collapsed to expanded and vice versa. -In the `render()` method, use a `
` element with the appropriate style. ```css @@ -48,76 +47,53 @@ div.tree-element:before { ``` ```jsx -class TreeView extends React.Component { - constructor(props) { - super(props); - this.state = { - toggled: props.toggled - }; - this.toggle = this.toggle.bind(this); - } +function TreeView({ + data, + toggled = true, + name = null, + isLast = true, + isChildElement = false, + isParentToggled = true +}) { + const [isToggled, setIsToggled] = React.useState(toggled); - toggle() { - this.setState(state => ({ toggled: !state.toggled })); - } - - render() { - return ( -
- {Array.isArray(this.props.data) ? "" : {v}: } - {this.props.data[v]} - {i === a.length - 1 ? "" : ","} -
- ) - )} - {Array.isArray(this.props.data) ? "]" : "}"} - {!this.props.isLast ? "," : ""} -+ {Array.isArray(data) ? "" : {v}: } + {data[v]} + {i === a.length - 1 ? "" : ","} +
+ ) + )} + {Array.isArray(data) ? "]" : "}"} + {!isLast ? "," : ""} +