ESlint 抱怨 prop 验证中缺少 this.props.handleSubmit
ESlint complains about this.props.handleSubmit missing in prop validation
我有一个名为 SiteTechnologyForm
的组件
class SiteTechnologyForm extends React.Component {
static propTypes = {
name: PropTypes.string,
site_technology_id_number: PropTypes.string,
common_site: PropTypes.string,
title: PropTypes.string,
errors: PropTypes.string
}
constructor(props) {
super(props);
this.state = {
logged_in: localStorage.getItem('token') ? true : false,
name: props.name || '',
site_technology_id_number: props.site_technology_id_number || '',
common_site: props.common_site || '',
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
UNSAFE_componentWillMount() {
if (!this.state.logged_in) {
browserHistory.push("/accounts/login");
}
}
handleChange(event) {
if (event.target && event.target.name){
this.setState({[event.target.name]: event.target.value});
}
}
handleSubmit() {
let payload = {
"common_site": this.state.common_site,
"name": this.state.name,
"site_technology_id_number": this.state.site_technology_id_number
};
this.props.handleSubmit(payload)
}
render() {
const {
title,
errors
} = this.props;
return !this.state.logged_in ? '' :
<BaseForm title={title} handleSubmit={this.handleSubmit} errors={errors} data={this.state}>
<CommonSiteAutocomplete
label="CommonSite *"
id="id_common_site"
required={true}
inputProps={{
name: "common_site",
onChange: this.handleChange,
value: this.state.common_site,
required: true
}}
/>
<CustomInput
labelText="Name *"
id="id_name"
formControlProps={{
fullWidth: true
}}
inputProps={{
type: "text",
onChange: this.handleChange,
name: "name",
value: this.state.name,
required: true
}}
/>
<CustomInput
labelText="Site Technology ID *"
id="id_site_technology_id_number"
formControlProps={{
fullWidth: true
}}
inputProps={{
type: "text",
onChange: this.handleChange,
name: "site_technology_id_number",
value: this.state.site_technology_id_number,
required: true
}}
/>
</BaseForm>
}
}
并且 ESlint 抱怨 handleSubmit
在 props 验证中丢失。
SiteTechnologyForm 用于其他组件,例如下面的 AddSiteTechnology
:
class AddSiteTechnology extends React.Component {
constructor(props) {
super(props);
this.state = {
errors: ''
};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(data) {
fetch(siteTechUrl, {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${localStorage.getItem('token')}`
},
method: 'POST',
body: JSON.stringify(data),
})
.then(response => {
if (response.status >= 400) {
return this.setState({errors: response.statusText});
}
browserHistory.push("/site_technologies/list");
})
}
render() {
return <SiteTechnologyForm title="Add Site Technology" handleSubmit={this.handleSubmit} errors={this.state.errors}/>
}
}
我最初尝试使用
handleSubmit: PropTypes.function
但是当我尝试 运行 网页时,我在控制台中收到此错误
checkPropTypes.js:19 警告:道具类型失败:SiteTechnologyForm:道具类型 handleSubmit
无效;它必须是一个函数,通常来自 prop-types
包,但收到 undefined
.
如何解决 handleSubmit 未通过验证的情况?
根据 ESLINT,您还需要为 handleSubmit 道具提供道具类型。函数类型由 func
而不是 function
:
处理
handleSubmit: PropTypes.func
您尚未在 propTypes
声明列表中定义 handleSubmit
类型,eslint
找不到 handleSubmit
。
将 handleSubmit
定义为 function
类型,我相信它总是需要的,因此也添加 isRequired
。
static propTypes = {
name: PropTypes.string,
site_technology_id_number: PropTypes.string,
common_site: PropTypes.string,
title: PropTypes.string,
errors: PropTypes.string,
handleSubmit: PropTypes.func.isRequired
}
我有一个名为 SiteTechnologyForm
class SiteTechnologyForm extends React.Component {
static propTypes = {
name: PropTypes.string,
site_technology_id_number: PropTypes.string,
common_site: PropTypes.string,
title: PropTypes.string,
errors: PropTypes.string
}
constructor(props) {
super(props);
this.state = {
logged_in: localStorage.getItem('token') ? true : false,
name: props.name || '',
site_technology_id_number: props.site_technology_id_number || '',
common_site: props.common_site || '',
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
UNSAFE_componentWillMount() {
if (!this.state.logged_in) {
browserHistory.push("/accounts/login");
}
}
handleChange(event) {
if (event.target && event.target.name){
this.setState({[event.target.name]: event.target.value});
}
}
handleSubmit() {
let payload = {
"common_site": this.state.common_site,
"name": this.state.name,
"site_technology_id_number": this.state.site_technology_id_number
};
this.props.handleSubmit(payload)
}
render() {
const {
title,
errors
} = this.props;
return !this.state.logged_in ? '' :
<BaseForm title={title} handleSubmit={this.handleSubmit} errors={errors} data={this.state}>
<CommonSiteAutocomplete
label="CommonSite *"
id="id_common_site"
required={true}
inputProps={{
name: "common_site",
onChange: this.handleChange,
value: this.state.common_site,
required: true
}}
/>
<CustomInput
labelText="Name *"
id="id_name"
formControlProps={{
fullWidth: true
}}
inputProps={{
type: "text",
onChange: this.handleChange,
name: "name",
value: this.state.name,
required: true
}}
/>
<CustomInput
labelText="Site Technology ID *"
id="id_site_technology_id_number"
formControlProps={{
fullWidth: true
}}
inputProps={{
type: "text",
onChange: this.handleChange,
name: "site_technology_id_number",
value: this.state.site_technology_id_number,
required: true
}}
/>
</BaseForm>
}
}
并且 ESlint 抱怨 handleSubmit
在 props 验证中丢失。
SiteTechnologyForm 用于其他组件,例如下面的 AddSiteTechnology
:
class AddSiteTechnology extends React.Component {
constructor(props) {
super(props);
this.state = {
errors: ''
};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(data) {
fetch(siteTechUrl, {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${localStorage.getItem('token')}`
},
method: 'POST',
body: JSON.stringify(data),
})
.then(response => {
if (response.status >= 400) {
return this.setState({errors: response.statusText});
}
browserHistory.push("/site_technologies/list");
})
}
render() {
return <SiteTechnologyForm title="Add Site Technology" handleSubmit={this.handleSubmit} errors={this.state.errors}/>
}
}
我最初尝试使用
handleSubmit: PropTypes.function
但是当我尝试 运行 网页时,我在控制台中收到此错误
checkPropTypes.js:19 警告:道具类型失败:SiteTechnologyForm:道具类型 handleSubmit
无效;它必须是一个函数,通常来自 prop-types
包,但收到 undefined
.
如何解决 handleSubmit 未通过验证的情况?
根据 ESLINT,您还需要为 handleSubmit 道具提供道具类型。函数类型由 func
而不是 function
:
handleSubmit: PropTypes.func
您尚未在 propTypes
声明列表中定义 handleSubmit
类型,eslint
找不到 handleSubmit
。
将 handleSubmit
定义为 function
类型,我相信它总是需要的,因此也添加 isRequired
。
static propTypes = {
name: PropTypes.string,
site_technology_id_number: PropTypes.string,
common_site: PropTypes.string,
title: PropTypes.string,
errors: PropTypes.string,
handleSubmit: PropTypes.func.isRequired
}