将类组件转换为函数组件后出现问题



在将该文件中的所有内容转换为函数后,我遇到了一些错误,因此我可以使用钩子。

当我按下calendarStrip组件上的日期时,我得到了这个错误"2020-12-22 is not a function

调用堆栈。告诉我问题出在onDateSelected上,你可以在下面的代码中找到。这让我怀疑,既然类组件中的一切都可以工作,我是否把useStates搞砸了?

使用USESTATE和calendardstrip组件的代码

const [datesWhitelist] = useState([
{
start: moment(),
end: moment().add(365, "days"),
},
]);
const [todoList] = useState([]);
const [markedDate] = useState([]);
const [currentDate] = useState(
`${moment().format("YYYY")}-${moment().format("MM")}-${moment().format(
"DD"
)}`
);
const [selectedTask] = useState(null);
const [isDateTimePickerVisible] = useState(false);
const [createEventAsyncRes] = useState("");
<CalendarStrip
calendarAnimation={{ type: "sequence", duration: 30 }}
style={{
height: 150,
paddingTop: 20,
paddingBottom: 20,
}}
calendarHeaderStyle={{ color: colors.text }}
dateNumberStyle={{ color: colors.text, paddingTop: 10 }}
dateNameStyle={{ color: colors.text }}
highlightDateNumberStyle={{
color: colors.text,
backgroundColor: "#990000",
marginTop: 10,
height: 35,
width: 35,
textAlign: "center",
borderRadius: 17.5,
overflow: "hidden",
paddingTop: 6,
fontWeight: "400",
justifyContent: "center",
alignItems: "center",
}}
highlightDateNameStyle={{ color: colors.text }}
disabledDateNameStyle={{ color: colors.text }}
disabledDateNumberStyle={{ color: colors.text }}
datesWhitelist={datesWhitelist}
iconContainer={{ flex: 0.1 }}
markedDates={markedDate}
onDateSelected={(date) => {
const selectedDate = `${moment(date).format("YYYY")}-${moment(
date
).format("MM")}-${moment(date).format("DD")}`;
_updateCurrentTask(selectedDate);
currentDate(selectedDate);
}}
/>

这就是问题的来源:

currentDate(selectedDate);

如果使用字符串作为函数,请查看如何在状态中声明它

最新更新