0

我目前正在尝试将 react-sortable-hoc 添加到列表组件中,当我通过道具更改传入的项目列表时遇到了许多错误。例如:如果我向数组中添加一个项目,并检查列表的传入道具,可排序组件中的道具正在更改(我在列表组件道具中看到新项目),但我无法获取重新渲染的组件。我尝试过 shouldComponentUpdate、getDerivedStateFromProps、使用 PureComponent 而不是 Component、forceUpdate、hook 整个事情并为 props 的任何更改设置 useEffect,没有任何事情导致重新渲染,我知道新列表正在进入组件。

有没有其他人有同样的问题,或者有没有人找到解决方法?

这是我设置道具/状态的方式,以及我如何知道道具正在改变:

class MobileListItems extends Component {

  state = {
    items: this.props.listItems,
  }

.
.
.

  render() {

    console.log('this.props.listItems', this.props.listItems)
    console.log('this.state.items', this.state.items)

在我对一些东西进行排序并尝试添加另一个项目之后,this.props.listItems它会增加 1,但无论我尝试过上述哪种方法,都不会重新渲染。

4

1 回答 1

0

哎呀雏菊!不要将你的道具复制到状态中,你们。不要像我一样!

删除状态管理并将其变成一个函数修复了我遇到的所有错误。

于 2020-09-25T22:39:52.740 回答