我的代码如下 -
主页.js
import React from 'react'
import AddNewTask from './../components/addNewTask'
class Home extends React.Component{
constructor(props){
super(props)
this.state = {
addNewTask:{
title:'',
discription:'',
deadline:new Date()
},
}
this.handleNewTaskChange = this.handleNewTaskChange.bind(this)
}
handleNewTaskChange(event){
//console.log(typeof(event))
var evName,evValue;
if(event.target){
const{name,value} = event.target
console.log(name,value)
evName = name
evValue = value
}
else{
evName = "deadline"
evValue = event
}
//console.log(evName,evValue)
this.setState({
addNewTask:{
[evName]:evValue
}
})
}
render(){
return(
<div>
<AddNewTask
newTask = {this.state.addNewTask}
handleChange = {this.handleNewTaskChange}
/>
</div>
)
}
}
addNewTask.js
import React from 'react';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
function AddNewTask(props) {
return (
<div>
<form>
<label>Task : </label>
<input
name='title'
placeholder='Enter Task'
value={props.newTask.title}
onChange={props.handleChange}
/>
<br />
<label>Discription : </label>
<textarea
name='discription'
placeholder='Enter Task Discription'
value={props.newTask.discription}
onChange={props.handleChange}
/>{' '}
<br />
<label>Deadline : </label>
<DatePicker
name='deadline'
selected={props.newTask.deadline}
onChange={(date) => props.handleChange(date)}
minDate={new Date()}
/>
</form>
</div>
);
}
export default AddNewTask;
我收到了这个错误-
index.js:1 警告:组件正在将文本类型的受控输入更改为不受控制。输入元素不应从受控切换到不受控(反之亦然)。决定在组件的生命周期内使用受控输入元素还是不受控输入元素。更多信息:https ://reactjs.org/docs/forms.html#control-components
另外,我还没有添加提交功能。