有必要从道具的子组件中传递出来,这是一个改变,但是对派克的验证消失了,状态没有改变
import { KeyboardDatePicker } from "@material-ui/pickers";
import MomentUtils from "@date-io/moment";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
const Picker = ({dateOnChange,dateOfOrdinance}) => {
<MuiPickersUtilsProvider utils={MomentUtils}>
<KeyboardDatePicker
inputVariant="outlined"
clearable
label="От:"
value={dateOfOrdinance}
onChange={dateOnChange}
format="YYYY-MM-DD"
InputAdornmentProps={{ position: "start" }}
/>
</MuiPickersUtilsProvider>
}
我的父组件
export default class Case extends Component {
state={dateOfOrdinance: new Date()}
render() {
return (
<Picker dateOnChange={(date)=>
this.setState({dateOfOrdinance: date}) />
)
}
}
如果我在父级中声明它,那么一切都会好起来,将进行验证并且状态会发生变化
答案 0 :(得分:2)
这行有问题,
<Picker dateOnChange={(date)=> this.setState({dateOfOrdinance: date}) />
您尚未在此处传递dateOfOrdinance
作为道具,请执行此操作以传递数据
<Picker dateOfOrdinance={this.satate.dateOfOrdinance} dateOnChange={(date)=> this.setState({dateOfOrdinance: date}) />