I am new to learning react and I am having a tough time converting class components into functional components for understanding problems and analyzing . I want to use functional approach for understanding states and life cycle methods how that works in hooks concept .
How we can achieve this?
my code in class are
import React, { Component } from 'react';
import SortableTree, { toggleExpandedForAll } from 'react-sortable-tree';
import CustomTheme from '../index';
import './app.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
searchString: '',
searchFocusIndex: 0,
searchFoundCount: null,
treeData: [
{ title: 'This is the Full Node Drag theme' },
{ title: 'You can click anywhere on the node to drag it' },
{
title: 'This node has dragging disabled',
subtitle: 'Note how the hover behavior is different',
dragDisabled: true,
},
{ title: 'Chicken', children: [{ title: 'Egg' }] },
],
};
this.updateTreeData = this.updateTreeData.bind(this);
this.expandAll = this.expandAll.bind(this);
this.collapseAll = this.collapseAll.bind(this);
}
updateTreeData(treeData) {
this.setState({ treeData });
}
expand(expanded) {
this.setState({
treeData: toggleExpandedForAll({
treeData: this.state.treeData,
expanded,
}),
});
}
expandAll() {
this.expand(true);
}
collapseAll() {
this.expand(false);
}
render() {
const {
treeData,
searchString,
searchFocusIndex,
searchFoundCount,
} = this.state;
const alertNodeInfo = ({ node, path, treeIndex }) => {
const objectString = Object.keys(node)
.map(k => (k === 'children' ? 'children: Array' : `${k}: '${node[k]}'`))
.join(',\n ');
global.alert(
'Info passed to the icon and button generators:\n\n' +
`node: {\n ${objectString}\n},\n` +
`path: [${path.join(', ')}],\n` +
`treeIndex: ${treeIndex}`
);
};
const selectPrevMatch = () =>
this.setState({
searchFocusIndex:
searchFocusIndex !== null
? (searchFoundCount + searchFocusIndex - 1) % searchFoundCount
: searchFoundCount - 1,
});
const selectNextMatch = () =>
this.setState({
searchFocusIndex:
searchFocusIndex !== null
? (searchFocusIndex + 1) % searchFoundCount
: 0,
});
return (
<div
style={{ display: 'flex', flexDirection: 'column', height: '100vh' }}
>
<div style={{ flex: '0 0 auto', padding: '0 15px' }}>
<h3>Full Node Drag Theme</h3>
<button onClick={this.expandAll}>Expand All</button>
<button onClick={this.collapseAll}>Collapse All</button>
<form
style={{ display: 'inline-block' }}
onSubmit={event => {
event.preventDefault();
}}
>
<label htmlFor="find-box">
Search:
<input
id="find-box"
type="text"
value={searchString}
onChange={event =>
this.setState({ searchString: event.target.value })
}
/>
</label>
<button
type="button"
disabled={!searchFoundCount}
onClick={selectPrevMatch}
>
<
</button>
<button
type="submit"
disabled={!searchFoundCount}
onClick={selectNextMatch}
>
>
</button>
<span>
{searchFoundCount > 0 ? searchFocusIndex + 1 : 0}
/
{searchFoundCount || 0}
</span>
</form>
</div>
<div style={{ flex: '1 0 50%', padding: '0 0 0 15px' }}>
<SortableTree
theme={CustomTheme}
treeData={treeData}
onChange={this.updateTreeData}
searchQuery={searchString}
searchFocusOffset={searchFocusIndex}
style={{width: '600px'}}
rowHeight={45}
searchFinishCallback={matches =>
this.setState({
searchFoundCount: matches.length,
searchFocusIndex:
matches.length > 0 ? searchFocusIndex % matches.length : 0,
})
}
canDrag={({ node }) => !node.dragDisabled}
generateNodeProps={rowInfo => ({
buttons: [
<button onClick={() => alertNodeInfo(rowInfo)}>i</button>,
],
})}
/>
</div>
</div>
);
}
}
export default App;