【问题标题】:React Native Date Range反应本机日期范围
【发布时间】:2020-05-07 21:12:44
【问题描述】:
import React, {useState} from "react";
import { StyleSheet, View, Text } from "react-native";
import { globalStyles } from "../styles/global";
import {Calendar, CalendarList, Agenda} from 'react-native-calendars';
import {LocaleConfig} from 'react-native-calendars';
import  moment  from  "moment";
import DateRangePicker from "react-native-daterange-picker";

export default function About(){
    const [endDate, setendDate] = useState(null)
    const [startDate, setstartDate] = useState(null)
    const [displayedDate, setdisplayedDate] = useState(moment())

    state = {
        endDate: null,
        startDate: null,
        displayedDate: moment()
    };

    const handleSubmit = (props) => {

        console.log(props);
        setendDate(props.endDate);
        setstartDate(props.startDate);
        setdisplayedDate(props.displayedDate);
        // console.log(props.startDate);
        // console.log(props.displayedDate);
    }

    return(
        <View style={globalStyles.container}>

        <DateRangePicker
          onChange={ handleSubmit }
          endDate={endDate}
          startDate={startDate}
          displayedDate={displayedDate}
          range>
          <Text>Click me!</Text>
          </DateRangePicker>

        </View>
    )
}

1.无法选择日期范围。 2. undefined is not an object(评估displayedDate.format) 3. 使用函数组件,但大多数解决方案都可以使用类组件

【问题讨论】:

  • 更改了一些代码
  • 错误 2. 告诉您它无法评估 displayedDate.format 这告诉您它正在尝试对字符串使用矩方法。试试这个。 setdisplayedDate(moment(props.displayedDate)); 其余的。我在同一条船上。

标签: react-native date-range


【解决方案1】:

你可以如下调用handleSubmit onChang={() => 处理提交()}

并且函数原型中不需要props参数 const handleSubmit = (props) => {} 因为 props 已经声明了,你不需要将它设置为参数。 如果你想用它作为参数,那么你应该像这样改变 onChange={() => handleSubmit(props)}

希望对你有所帮助。

【讨论】:

  • 仍然无法正常工作....我认为我在设置 endDate、startDate 和 displayDate 的值时犯了一些错误
  • 时刻默认格式为 YYYY-MM-DDTHH:mm:ssZ。所以请检查日期变量的格式是否如上。
猜你喜欢
  • 2020-10-12
  • 2021-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 2021-10-06
相关资源
最近更新 更多