this.setState 不是函数,但应该在范围内
this.setState is not a function but should be within scope
鉴于我的组件,当我尝试更改输入时出现错误 "this.setState() is not a function." 据我所知,它在范围内,所以我不确定为什么找不到它。我尝试在构造函数中绑定函数,但它产生了相同的结果。
import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
class UpdateTab extends Component {
constructor(props) {
super(props);
const {settings} = this.props;
this.state = {
version_text: "",
version_description: "",
accepted: [],
post_changes_to_comments: settings.post_changes_to_comments,
};
}
componentDidMount(){
const {settings} = this.props;
this.setState = ({
post_changes_to_comments: settings.post_changes_to_comments,
});
}
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(e){
console.log("working");
e.preventDefault();
const values = {
version_number: this.state.version_text,
version_description: this.state.version_description,
file: this.state.accepted
};
this.props.saveUpdate(values)
}
render() {
const placeholder = "This section is to highlight the changes in the updated \n" +
"version of the document.\n" +
"\n" +
"This will appear in the comment section unless disabled within the “Edit” tab.";
return (
<div className="settings-panel">
<form>
<table>
<tbody>
<tr>
<td>Version Number</td>
<td rowSpan={4}>
</td>
</tr>
<tr>
<td>
<input value={this.state.version_text}
onChange={(e) => this.handleInputChange(e)}
name="version_text"
placeholder="eg. 1.0"/>
</td>
</tr>
<tr>
<td>Description of Changes</td>
</tr>
<tr>
<td>
<textarea value={this.state.version_description}
className="description-of-changes-textarea"
onChange={(e) => this.handleInputChange(e)}
name="version_description"
placeholder={placeholder}/>
</td>
</tr>
<tr>
<td>
<input name="post_changes_to_comments"
type="checkbox"
checked={!this.state.post_changes_to_comments}
onChange={(e) => this.handleInputChange(e)}/>
<label htmlFor="post_changes_to_comments">Do not display this description of changes in SRD comments</label>
</td>
</tr>
</tbody>
</table>
<button className="button-filled center" onClick={(e) => this.handleSubmit(e)}>Update</button>
</form>
</div>
);
}
}
function mapStateToProps(state) {
return {
settings: state.settings,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
saveUpdate: saveUpdate,
}, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(UpdateTab);
对于为什么 "this.setState" 在我的输入更改处理程序中不可见的任何帮助,我们将不胜感激。
这里
this.setState = ({
post_changes_to_comments: settings.post_changes_to_comments,
});
您正在覆盖 setState
。你知道,搬到
this.setState({
post_changes_to_comments: settings.post_changes_to_comments,
});
鉴于我的组件,当我尝试更改输入时出现错误 "this.setState() is not a function." 据我所知,它在范围内,所以我不确定为什么找不到它。我尝试在构造函数中绑定函数,但它产生了相同的结果。
import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
class UpdateTab extends Component {
constructor(props) {
super(props);
const {settings} = this.props;
this.state = {
version_text: "",
version_description: "",
accepted: [],
post_changes_to_comments: settings.post_changes_to_comments,
};
}
componentDidMount(){
const {settings} = this.props;
this.setState = ({
post_changes_to_comments: settings.post_changes_to_comments,
});
}
handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(e){
console.log("working");
e.preventDefault();
const values = {
version_number: this.state.version_text,
version_description: this.state.version_description,
file: this.state.accepted
};
this.props.saveUpdate(values)
}
render() {
const placeholder = "This section is to highlight the changes in the updated \n" +
"version of the document.\n" +
"\n" +
"This will appear in the comment section unless disabled within the “Edit” tab.";
return (
<div className="settings-panel">
<form>
<table>
<tbody>
<tr>
<td>Version Number</td>
<td rowSpan={4}>
</td>
</tr>
<tr>
<td>
<input value={this.state.version_text}
onChange={(e) => this.handleInputChange(e)}
name="version_text"
placeholder="eg. 1.0"/>
</td>
</tr>
<tr>
<td>Description of Changes</td>
</tr>
<tr>
<td>
<textarea value={this.state.version_description}
className="description-of-changes-textarea"
onChange={(e) => this.handleInputChange(e)}
name="version_description"
placeholder={placeholder}/>
</td>
</tr>
<tr>
<td>
<input name="post_changes_to_comments"
type="checkbox"
checked={!this.state.post_changes_to_comments}
onChange={(e) => this.handleInputChange(e)}/>
<label htmlFor="post_changes_to_comments">Do not display this description of changes in SRD comments</label>
</td>
</tr>
</tbody>
</table>
<button className="button-filled center" onClick={(e) => this.handleSubmit(e)}>Update</button>
</form>
</div>
);
}
}
function mapStateToProps(state) {
return {
settings: state.settings,
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({
saveUpdate: saveUpdate,
}, dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(UpdateTab);
对于为什么 "this.setState" 在我的输入更改处理程序中不可见的任何帮助,我们将不胜感激。
这里
this.setState = ({
post_changes_to_comments: settings.post_changes_to_comments,
});
您正在覆盖 setState
。你知道,搬到
this.setState({
post_changes_to_comments: settings.post_changes_to_comments,
});