我正在使用 React.js,我想在类中转换我的函数。这是我的代码:
import React, {useState} from "react";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import classes from "./Datepicker.css";
const Datepicker = () => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker className="custom-select" dateFormat="dd/MM/yyyy" selected={startDate}
onChange={date => setStartDate(date)}/>
);
};
export default Datepicker;
我这样想:
class Datepicker extends Component {
render(){
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker className="custom-select" dateFormat="dd/MM/yyyy" selected={startDate}
onChange={date => setStartDate(date)}/>
)
}
}
export default Datepicker;
但我得到了这个:
React Hook "useState" cannot be called in a class component.
请问你能帮帮我吗 ?
非常感谢 !
噜噜哒
莫回无
临摹微笑
相关分类