【发布时间】:2018-08-16 19:52:51
【问题描述】:
我想在 onClick 被触发后清除我的状态。但是,我的状态不会更新,而是呈现初始状态。
这里是代码
import React from 'react';
import {firebaseCon} from '../connection';
class Update extends React.Component {
constructor(props) {
super(props);
this.state = {
title: ""
};
this.updateItem = this.updateItem.bind(this);
this.handleChange = this.handleChange.bind(this);
}
componentWillReceiveProps(props) {
this.setState({
title: props.item.title
});
}
updateItem(itemId) {
let inputVal = document.getElementById("editData").value;
firebaseCon.content.update('text', itemId, { title: inputVal })
.then(() => console.log('Updating the entry succeeded'))
.catch((e) => console.error(e));
this.setState({ title: "" });
}
handleChange(e) {
var value = e.target.value;
this.setState({ title: value });
}
render() {
return (
<div className="input-field col s12">
<i className="material-icons prefix">mode_edit</i>
<input type="text" id="editData" value={this.state.title || ""} onChange={this.handleChange} />
<button className="waves-effect waves-light btn" onClick={this.updateItem.bind(this, this.props.item.id)}>UPDATE</button>
</div>
)
}
}
export default Update
我在另一个组件中有一个按钮,它在单击时设置道具,然后我的输入字段将其用作值。更改值时,状态会更新,工作正常。但是,当我然后单击更新按钮时,数据已正确更新,但状态未更新。
【问题讨论】:
-
firebaseCon.content.update函数内部执行了哪些操作?