Skip to content

Commit d770734

Browse files
committed
Update example to use functions and hooks
1 parent 4bbf36f commit d770734

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

README.md

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,34 @@ import 'react-checkbox-tree/lib/react-checkbox-tree.css';
4444

4545
### Render Component
4646

47-
A quick usage example is included below. Note that the react-checkbox-tree component is [controlled](https://facebook.github.io/react/docs/forms.html#controlled-components). In other words, you must update its `checked` and `expanded` properties whenever a change occurs.
47+
Below is a minimal example using [state hooks](https://reactjs.org/docs/hooks-state.html). Note that `CheckboxTree` is a [controlled](https://facebook.github.io/react/docs/forms.html#controlled-components) component, so you must update its `checked` and `expanded` properties whenever a change occurs.
4848

4949
``` jsx
50-
import React from 'react';
50+
import React, { useState } from 'react';
5151
import CheckboxTree from 'react-checkbox-tree';
5252

5353
const nodes = [{
54-
value: 'mars',
55-
label: 'Mars',
56-
children: [
57-
{ value: 'phobos', label: 'Phobos' },
58-
{ value: 'deimos', label: 'Deimos' },
59-
],
54+
value: 'mars',
55+
label: 'Mars',
56+
children: [
57+
{ value: 'phobos', label: 'Phobos' },
58+
{ value: 'deimos', label: 'Deimos' },
59+
],
6060
}];
6161

62-
class Widget extends React.Component {
63-
state = {
64-
checked: [],
65-
expanded: [],
66-
};
67-
68-
render() {
69-
return (
70-
<CheckboxTree
71-
nodes={nodes}
72-
checked={this.state.checked}
73-
expanded={this.state.expanded}
74-
onCheck={checked => this.setState({ checked })}
75-
onExpand={expanded => this.setState({ expanded })}
76-
/>
77-
);
78-
}
62+
function Widget() {
63+
const [checked, setChecked] = useState([]);
64+
const [expanded, setExpanded] = useState([]);
65+
66+
return (
67+
<CheckboxTree
68+
nodes={nodes}
69+
checked={checked}
70+
expanded={expanded}
71+
onCheck={(checked) => setChecked(checked)}
72+
onExpand={(expanded) => setExpanded(expanded)}
73+
/>
74+
);
7975
}
8076
```
8177

0 commit comments

Comments
 (0)