Reactjs React-组件在重新渲染后不会更新
重新渲染后,我的一个组件不会得到更新。我认为这只是偶尔发生的事 我检查了组件是否正确渲染。在下面的代码中,您将在Reactjs React-组件在重新渲染后不会更新,reactjs,redux,Reactjs,Redux,重新渲染后,我的一个组件不会得到更新。我认为这只是偶尔发生的事 我检查了组件是否正确渲染。在下面的代码中,您将在render函数中看到console.log。我意识到值的变化是正确的。但是组件本身并没有可视化地更新。我不认为我改变了任何状态/道具。我使用的大多数状态管理。你知道为什么会这样吗?多谢各位 子组件如下所示: export default ChildComponent extends React.Component { ... render() { ...
render
函数中看到console.log
。我意识到值的变化是正确的。但是组件本身并没有可视化地更新。我不认为我改变了任何状态/道具。我使用的大多数状态管理。你知道为什么会这样吗?多谢各位
子组件如下所示:
export default ChildComponent extends React.Component {
...
render() {
...
const barWidth = this.getProgressWidth(boundingClientRect) // also uses this.props.editorTimestamp
console.log(barWidth) // This value is updated like I expect
return (
<div>
...
<div style={{...styles.barStyles, width: barWidth }}>
...
</div>
)
}
}
export default ParentComponent extends React.Component {
...
render() {
...
const singleEditorTimestamp = this.getEditorTimestamp(id) // gets the timestamp from timestamps obj
return (
<div>
...
<ChildComponent editorTimestamp={singleEditorTimestamp} />
...
</div>
)
}
}
导出默认子组件扩展React.Component{
...
render(){
...
const barWidth=this.getProgressWidth(boundingClientRect)//也使用this.props.editorTimestamp
console.log(barWidth)//此值按预期更新
返回(
...
...
)
}
}
父组件如下所示:
export default ChildComponent extends React.Component {
...
render() {
...
const barWidth = this.getProgressWidth(boundingClientRect) // also uses this.props.editorTimestamp
console.log(barWidth) // This value is updated like I expect
return (
<div>
...
<div style={{...styles.barStyles, width: barWidth }}>
...
</div>
)
}
}
export default ParentComponent extends React.Component {
...
render() {
...
const singleEditorTimestamp = this.getEditorTimestamp(id) // gets the timestamp from timestamps obj
return (
<div>
...
<ChildComponent editorTimestamp={singleEditorTimestamp} />
...
</div>
)
}
}
导出默认父组件扩展React.Component{
...
render(){
...
const singleEditorTimestamp=this.getEditorTimestamp(id)//从timestamps obj获取时间戳
返回(
...
...
)
}
}
状态
或道具
更改可触发渲染。还有this.forceUpdate()代码>
你可以像我一样
const barWidth = this.getProgressWidth(boundingClientRect)
this.setState({barWidth: barWidth});
但改变渲染内部的状态不是个好主意。因为它会触发无限的渲染过程。您应该声明一个事件
,并在该事件中更改状态
。) 状态
或道具
更改可以触发渲染。还有this.forceUpdate()代码>
你可以像我一样
const barWidth = this.getProgressWidth(boundingClientRect)
this.setState({barWidth: barWidth});
但改变渲染内部的状态不是个好主意。因为它会触发无限的渲染过程。您应该声明一个事件
,并在该事件中更改状态
。) 也许明智的做法是将barWidth
存储在状态中,这样它会随着触发重新渲染的状态更改而更新。你是对的。道具更改也会触发重新渲染。PureComponent可以重新渲染,具体取决于是否通过进行浅层比较(参考差异)来更改道具。可能不是一个好的答案,所以我删除了它,因为它不能解决您的问题。@沉默很好,我可以这样做,但它会在生命周期中添加一个额外的setState
。而且barWidth
变化太大,所以我不想这样做。此外,这种情况很少发生。您确定您发送了正确的操作,并且redux在您的情况下会正常工作吗?请更新您的帖子并添加减速机以及应用程序连接到的方式redux@novonimo我添加了它,也许将barWidth
存储在状态中是明智的,这样它会随着触发重新渲染的状态更改而更新。你是对的。道具更改也会触发重新渲染。PureComponent可以重新渲染,具体取决于是否通过进行浅层比较(参考差异)来更改道具。可能不是一个好的答案,所以我删除了它,因为它不能解决您的问题。@沉默很好,我可以这样做,但它会在生命周期中添加一个额外的setState
。而且barWidth
变化太大,所以我不想这样做。此外,这种情况很少发生。您确定您发送了正确的操作,并且redux在您的情况下会正常工作吗?请更新您的帖子并添加减速机以及应用程序连接到的方式redux@novonimo我添加了itI不使用setState
内部render
函数。实际上现在,我正试图避免在componentdiddupdate
中使用它。因为,在我的代码块中,this.props.editorTimestamp
发生了变化。该组件被渲染(我可以从渲染函数中的console.log中看到),但是它不会被可视化地更新。我不明白为什么会这样。哦,我错了。下面是你想要做的:谢谢你的帖子,但是我的进度条在工作。我只是想弄明白为什么它有时不响应道具的变化。我不使用setState
insiderender
函数。实际上现在,我正试图避免在componentdiddupdate
中使用它。因为,在我的代码块中,this.props.editorTimestamp
发生了变化。该组件被渲染(我可以从渲染函数中的console.log中看到),但是它不会被可视化地更新。我不明白为什么会这样。哦,我错了。下面是你想要做的:谢谢你的帖子,但是我的进度条在工作。我只是想弄明白为什么有时候它对道具的改变没有反应。