我使用 datepicker 添加项目并且它可以工作,但是当我尝试使用 datepicker 编辑/更新这些项目时,它不起作用!给我很多错误,例如:
失败的道具类型:提供给 的道具类型无效
selected
,string
应DatePicker
为object
组件正在更改要控制的文本类型的不受控输入。输入元素不应从不受控切换到受控(反之亦然)。决定在组件的生命周期内使用受控输入元素还是不受控输入元素。
道具类型失败:提供给
selected
的道具类型无效,预期为。string
Calendar/Month/Week/Day
object
未捕获的类型错误:无法读取未定义的属性“值”
这是我的 EditProject 组件:
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
import DatePicker from 'react-datepicker';
import moment from 'moment';
import './EditProject.css';
import 'react-datepicker/dist/react-datepicker.css';
class EditProject extends Component {
constructor(props) {
super(props);
this.state = {
project: {}
}
}
componentDidMount() {
// console.log('PROPS ' + JSON.stringify(this.props));
const { match: { params } } = this.props;
fetch(`/dashboard/project/${params.id}/edit`)
.then(response => {
return response.json()
}).then(project => {
this.setState({
projectname: project.projectname,
typeofproject: project.typeofproject,
imageURL: project.imageURL,
startDate: project.startDate,
endDate: project.endDate
})
})
}
render() {
const { match: { params } } = this.props;
return (
<div className='EditProject'>
<h1 className='EditProject__title'>Edit</h1><h1 className='EditProject__projectname'>{this.state.projectname}</h1>
<hr />
<form method='POST' action={`/dashboard/project/${params.id}/edit?_method=PUT`}>
<div className='form-group container'>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" style={{border:'none'}} id="basic-addon1">Project Name</span>
</div>
<input type='text' className='form-control input' placeholder='insert new project name' value={this.state.projectname} name='projectname' ref='projectname' onChange={(event) => this.setState({ projectname: event.target.value })} />
</div>
</div>
<div className='form-group container'>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" style={{border:'none'}} id="basic-addon1">Type of Production</span>
</div>
<select className='form-control input' value={this.state.typeofproject} name='typeofproject' ref='typeofproject' onChange={(event) => this.setState({ typeofproject: event.target.value })}>
<option value='Film (Cinema)'>Film (Cinema)</option>
<option value='Film (TV)'>Film (TV)</option>
<option value='Film (series)'>Film (series)</option>
<option value='Short film'>Short film</option>
<option value='TV mini-series'>TV mini-series</option>
<option value='TV series'>TV series</option>
<option value='TV program'>TV program</option>
<option value='TV reporting'>TV reporting</option>
<option value='Vox Pop'>Vox Pop</option>
<option value='Advertisement Ad'>Advertisement Ad</option>
<option value='Documentary'>Documentary</option>
<option value='Documentary (TV)'>Documentary (TV)</option>
<option value='Documentary (series)'>Documentary (series)</option>
<option value='Commercial'>Commercial</option>
<option value='Video Clip'>Video Clip</option>
<option value='Live Video Clip TV'>Live Video Clip TV</option>
<option value='Photography Session'>Photography Session</option>
</select>
</div>
</div>
<div className='form-group container'>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" style={{border:'none'}} id="basic-addon1">Start Date</span>
</div>
// ================DATEPICKER HERE================
<DatePicker
todayButton={"Today"}
dateFormat="DD/MM/YYYY"
selected={this.state.startDate}
value={this.state.startDate}
onChange={(event) => this.setState({ startDate: event.target.value })}
/>
</div>
</div>
<div className='form-group container'>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" style={{border:'none'}} id="basic-addon1">End Date</span>
</div>
<DatePicker
todayButton={"Today"}
dateFormat="DD/MM/YYYY"
selected={this.state.endDate}
value={this.state.endDate}
onChange={(event) => this.setState({ endDate: event.target.value })}
/>
</div>
</div>
// / ================DATEPICKER HERE================
<div className='form-group container'>
<div className="input-group mb-3">
<div className="input-group-prepend">
<span className="input-group-text" style={{border:'none'}} id="basic-addon1">Project Image URL</span>
</div>
<input type='text' className='form-control input 'placeholder='insert new project image URL' value={this.state.imageURL} name='imageURL' ref='imageURL' onChange={(event) => this.setState({ imageURL: event.target.value })} />
</div>
</div>
<div className='form-group container'>
<button type='submit' className='btn btn-default button--update'>Update</button>
<NavLink to={`/project/${params.id}/`} ><button type='submit' style={{ backgroundColor: '#b5b5b5' }} className='btn btn-default button--update'>Cancel</button></NavLink>
</div>
</form>
</div>
);
}
}
export default EditProject;
非常感谢你的帮助!