setState()不触发重新渲染
这段代码有什么内在的错误吗?setState()不触发重新渲染
部分的问题是这样的:
// Update state and trigger re-render
// NOT WORKING
this.setState({ subsections });
国家没有被置位,当我打电话的setState()。尽管数据存在于subsections
对象中。我在其他方法上使用了相同的代码模式,并按预期工作。
数据也保存在Parse中,如果我刷新它的页面。
import React from "react";
import Parse from 'parse';
import Subsections from './Subsections';
import AddSubsectionForm from './AddSubsectionForm';
class Manage extends React.Component {
constructor() {
super();
this.addSubsection = this.addSubsection.bind(this);
// Set initial state
this.state = {
subsections: {}
};
}
addSubsection(subsection) {
// Make copy of state
const subsections = {...this.state.subsections};
// Create new object
var SubsectionTest = Parse.Object.extend('SubsectionTest');
var subsectionTest = new SubsectionTest();
// Save object, then update state on return
subsectionTest.save({
name: subsection.name,
description: subsection.description
}).then(function(newSubsection) {
console.log('Has saved id of: '+newSubsection.id);
// Add new subsection to local state
subsections[newSubsection.id] = subsection;
// Log updatd subsections object
console.log(subsections);
// Update state and trigger re-render
// NOT WORKING
this.setState({ subsections });
},
function(error) {
console.log('Error:');
console.log(error);
});
}
/*
Loads subsections from Parse and displays them.
Code removed for clarity.
*/
componentWillMount() {
this.loadMenuItems();
}
render() {
if (this.state.subsections) {
return (
<div className="subsections">
<div className="mt3 border-top">
<h3 className="mt1 bold s2">Subsections</h3>
<div className="mt1">
<Subsections subsections={this.state.subsections} />
</div>
</div>
<div className="mt3 border-top">
<AddSubsectionForm addSubsection={this.addSubsection}/>
</div>
</div>
)
}
}
}
export default Manage;
您的问题是this
未在承诺的then
处理程序的约束:
}).then(function(newSubsection) {
由于您使用的ES2015班,你大概可以使用箭头功能(结合this
其词法范围):
}).then(newSubSection => {
另一种选择是将其明确绑定:
}).then(function(newSubsection) {
...
}.bind(this));
我实现了箭头函数,但它仍然不更新状态。我忘了提及我之前已经明确地将它像第二个例子那样绑定了。特别是对于我来说很奇怪,因为同一个方法在同一个组件中的另一个函数中产生了预期的结果。 –
_anything_发生了什么?控制台中的错误消息? – rossipedia
好吧,现在看起来'render(){... return(...'在保存后(基于控制台记录消息)再次运行,而不是更新后的状态console.log显示'subsections'有新的对象,但是当我检查组件时,它没有State中的新对象 –
的this
的上下文丢失,你可以尝试回调结合this
var callback = function (newSubsection) {
console.log('Has saved id of: ' + newSubsection.id);
.....
this.setState({
subsections
});
};
callback = callback.bind(this);
subsectionTest.save({
name: subsection.name,
description: subsection.description
}).then(callback),
function (error) {
console.log('Error:');
console.log(error);
});
如果您正在使用Babel preset stage 2您可以通过使用箭头功能达致这
description: subsection.description
}).then(newSubsection = > {
console.log('Has saved id of: ' + newSubsection.id);
'.then(newSubsection =()=> {'给出'newSubsection not defined'错误, h让我觉得我没有使用Babel预设阶段2? (对不起,不是先进的开发者,更多的是设计师) –
我认为他的意思是'.then(newSubsection => {' – rossipedia
@rossipedia感谢您纠正:) – StateLess
你应该'else {return null; }'在你的render()函数中(不是答案,我知道它只是伸出在我身上) – rossipedia