I'm trying to migrate legacy code-base from class-based to function based using hooks.
The legacy code was like :
constructor(props){
super(props)
this.state = {
pickerProps: {
onPickDate: this.onPickDate,
activeColor: "#119955",
date: "12-12-2012"
},
inputStyle: "",
selectedDate: null
}
}
onPickDate = date => {
this.setState((state) => ({
selectedDate: date
}))
}
onInputChange = (prop, value) => {
this.setState((state) => ({
pickerProps: {
...state.pickerProps,
[prop]: value
}
}))
}
changeInputStyle = e => {
const value = e.target.value
this.setState((state) => ({inputStyle: value}))
}
and the new code is :
const [inputStyle, setInputStyle] = useState("");
const [selectedDate, setSelectedDate] = useState(null);
const [pickerProps, setPickerProps] = useState({
onPickDate:onPickDate, // How to pass function here
activeColor: "#119955",
date: "12-12-2012",
});
// This is the function I want to pass to useState()
const onPickDate = (date) => {
setSelectedDate(date);
};
const onInputChange = (prop, value) => {
setPickerProps({ [prop]: value });
};
const changeInputStyle = (e) => {
const value = e.target.value;
setInputStyle(value);
};
I receive a warning : The onPickDate was used before defined
Also when I send pickerProps to DatePicker :
<div className="date-picker">
<DatePicker {...pickerProps} />
</div>
It invokes an error :
OnPickDate is not defined ()
onPickDatedeclaration above theuseState?useCallback. Have not seen patterns of setting func to state. You can use it directly on render, whats the reason to put it in a state.. planning to modify it ?? 🤔