我目前正在尝试将react-sortable-hoc添加到一个列表组件中,当我通过props更改传入的条目列表时,我遇到了许多错误。例如:如果我将一个项目添加到数组中,并检查列表的传入道具,则可排序组件中的道具正在更改(我在列表组件道具中看到了新的项目),但我无法让组件重新呈现。我尝试过shouldComponentUpdate,getDerivedStateFromProps,使用PureComponent而不是Component,forceUpdate,将整个过程挂钩,并设置一个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,但无论我尝试了上面的哪种方法,都不会重新呈现。
发布于 2020-09-26 06:39:53
黛西!不要把你的道具复制到状态,你们所有人。别像我一样!
删除状态管理,并将其转换为一个函数,修复了我遇到的所有错误。
https://stackoverflow.com/questions/64001401
复制相似问题