我具有以下react组件,但是我找不到上面错误的原因,请帮助
import React, { Component } from 'react';
import { Input} from 'antd';
import Form from '../../components/uielements/form';
import Button from '../../components/uielements/button';
import Notification from '../../components/notification';
import { adalApiFetch } from '../../adalConfig';
const FormItem = Form.Item;
class CreateSiteCollectionForm extends Component {
constructor(props) {
super(props);
this.state = {Alias:'',DisplayName:'', Description:''};
this.handleChangeAlias = this.handleChangeAlias.bind(this);
this.handleChangeDisplayName = this.handleChangeDisplayName.bind(this);
this.handleChangeDescription = this.handleChangeDescription.bind(this);
};
handleChangeAlias(event){
this.setState({Alias: event.target.value});
}
handleChangeDisplayName(event){
this.setState({DisplayName: event.target.value});
}
handleChangeDescription(event){
this.setState({Description: event.target.value});
}
handleSubmit(e){
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
let data = new FormData();
//Append files to form data
data.append(JSON.stringify({"Alias": this.state.Alias,
"DisplayName": this.state.DisplayName,
"Description": this.state.Description
}));
const options = {
method: 'post',
body: data,
config: {
headers: {
'Content-Type': 'multipart/form-data'
}
}
};
adalApiFetch(fetch, "/SiteCollections", options)
.then(response =>{
if(response.status === 204){
Notification(
'success',
'Site collection created',
''
);
}else{
throw "error";
}
})
.catch(error => {
Notification(
'error',
'Site collection not created',
error
);
console.error(error);
});
}
});
}
render() {
const { getFieldDecorator } = this.props.form;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 14 },
},
};
const tailFormItemLayout = {
wrapperCol: {
xs: {
span: 24,
offset: 0,
},
sm: {
span: 14,
offset: 6,
},
},
};
return (
<Form onSubmit={this.handleSubmit}>
<FormItem {...formItemLayout} label="Alias" hasFeedback>
{getFieldDecorator('Alias', {
rules: [
{
required: true,
message: 'Please input your alias',
}
]
})(<Input name="alias" id="alias" onChange={this.handleChangeAlias} />)}
</FormItem>
<FormItem {...formItemLayout} label="Display Name" hasFeedback>
{getFieldDecorator('displayname', {
rules: [
{
required: true,
message: 'Please input your display name',
}
]
})(<Input name="displayname" id="displayname" onChange={this.handleChangedisplayname} />)}
</FormItem>
<FormItem {...formItemLayout} label="Description" hasFeedback>
{getFieldDecorator('description', {
rules: [
{
required: true,
message: 'Please input your description',
}
],
})(<Input name="description" id="description" onChange={this.handleChangeDescription} />)}
</FormItem>
<FormItem {...tailFormItemLayout}>
<Button type="primary" htmlType="submit">
Create modern site
</Button>
</FormItem>
</Form>
);
}
}
const WrappedCreateSiteCollectionForm = Form.create()(CreateSiteCollectionForm);
export default WrappedCreateSiteCollectionForm;
答案 0 :(得分:2)
只需为处理程序使用箭头功能即可避免this
上下文问题。
根据MDN web docs:
箭头函数表达式的语法比函数短 表达式,并且没有自己的this,arguments,super或 new.target。
因此,在您的组件中:
<Form onSubmit={(e) => this.handleSubmit(e)}>
...
onChange={(e) => this.handleChangeAlias(e)}
...
onChange={(e) => this.handleChangedisplayname(e)}
...
onChange={(e) => this.handleChangeDescription(e)}
而且,不要绑定构造函数:
constructor(props) {
super(props);
this.state = {Alias:'',DisplayName:'', Description:''};
};
内联函数和箭头函数的问题
关于该主题的文章很多,我不想在这里开始辩论,因为已经进行了很长时间的辩论。
如果您在使用此解决方案进行重新渲染时遇到问题,但是认为箭头功能更容易(编写,阅读,理解,绑定等),那么请看一下Reflective-bind,这可以解决这个问题很容易解决。
这些文章对于很好地了解内联函数和箭头函数的作用以及为什么在项目中使用或不使用它们很重要:
答案 1 :(得分:1)
您需要bind
handleSubmit
到this
this.handleSubmit = this.handleSubmit.bind(this)
在构造函数中
答案 2 :(得分:0)
像这样在构造函数中绑定handleSubmit
constructor(props) {
super(props);
this.state = {Alias:'',DisplayName:'', Description:''};
this.handleChangeAlias = this.handleChangeAlias.bind(this);
this.handleChangeDisplayName = this.handleChangeDisplayName.bind(this);
this.handleChangeDescription = this.handleChangeDescription.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); // Bind it
};