frontend-collective / frontend-collective/react-sortable-tree

[when using hooks] Cannot update a component from inside the function body of a different component.

Open
#687 5 comments 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5k
Forks
914
PR merge metrics
No merged PRs in 30d

Description

When storyboard class component search example is converted to functional (hooks) errors/warnings are thrown.

```
import React, { Component } from "react"
import SortableTree from "react-sortable-tree"
import "react-sortable-tree/style.css"

const data = [
{
title: "Windows 10",
subtitle: "running",
children: [
{
title: "Ubuntu 12",
subtitle: "halted",
children: [
{
title: "Debian",
subtitle: "gone"
}
]
},
{
title: "Centos 8",
subtitle: "hardening"
},
{
title: "Suse",
subtitle: "license"
}
]
}
]

const nodeInfo = row => console.log(row)

export default class App extends Component {
constructor(props) {
super(props)

this.state = {
searchString: "",
searchFocusIndex: 0,
searchFoundCount: null,
treeData: data
}
}

render() {
const { searchString, searchFocusIndex, searchFoundCount } = this.state

const customSearchMethod = ({ node, searchQuery }) =>
searchQuery &&
((node.title &&
node.title.toLowerCase().indexOf(searchQuery.toLowerCase()) > -1) ||
(node.subtitle &&
node.subtitle.toLowerCase().indexOf(searchQuery.toLowerCase()) > -1))

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 (


Find the needle!


{
event.preventDefault()
}}
>

this.setState({ searchString: event.target.value })
}
/>
 

<

 

>

 

 
{searchFoundCount > 0 ? searchFocusIndex + 1 : 0}
 / 
{searchFoundCount || 0}


this.setState({ treeData })}
searchMethod={customSearchMethod}
searchQuery={searchString}
searchFocusOffset={searchFocusIndex}
searchFinishCallback={matches =>
this.setState({
searchFoundCount: matches.length,
searchFocusIndex:
matches.length > 0 ? searchFocusIndex % matches.length : 0
})
}
generateNodeProps={row => {
return {
title: row.node.title,
subtitle: (
{row.node.subtitle}

),
buttons: [
nodeInfo(row)}
>


]
}
}}
/>


)
}
}
```
![image](https://user-images.githubusercontent.com/46776951/76942202-a1df4500-68fd-11ea-9ebf-f7e1368c8cd4.png)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.